我有兩個http請求,一個用curl寫,一個用javascript寫在Chrome中。 curl請求的作品,但鉻請求不,但我不知道爲什麼。兩個http請求之間的區別
鉻:
PUT /api/Account HTTP/1.1
Host: mydomain.co.nz
Connection: keep-alive
Content-Length: 152
Pragma: no-cache
Cache-Control: no-cache
Authorization: Bearer eyJhb..
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
Content-Type: multipart/form-data; boundary=--------------------------33e4cd665cd7a003
Accept: application/json
Referer: http://localhost:4200/conversations
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
--------------------------33e4cd665cd7a003
Content-Disposition: form-data; name="data"
{"isoncall" : true}
--------------------------33e4cd665cd7a003--
捲曲:
PUT /api/Account HTTP/1.1
Host: mydomain.co.nz
User-Agent: curl/7.51.0
accept: application/json
authorization: Bearer eyJhbGciO....
cache-control: no-cache
Content-Length: 158
Expect: 100-continue
Content-Type: multipart/form-data; boundary=--------------------
----a8c24af99c272f79
--------------------------a8c24af99c272f79
Content-Disposition: form-data; name="data"
{"isoncall" : true}
--------------------------a8c24af99c272f79--
我與鍍鉻得到的錯誤是:
[{ 「錯誤」:「流意外結束,內容可能已被其他組件讀取。「,」type「:」IOException「,」stack「:」at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.d__41.MoveNext()\ r \ n --- End從先前位置拋出異常的堆棧跟蹤--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n \ r \ n在Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.d__3.MoveNext()\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\ r \ n在Microsoft.AspNetCore.WebUtilities.MultipartReader.d__20.MoveNext()\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n ---結束堆棧跟蹤上一個拋出異常的位置--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n位於Microsoft的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n。 AspNetCore.Http.Features.FormFeature.d__18.MoveNext()\ r \ n ---堆棧跟蹤結束上一個拋出異常的位置--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n位於Microsoft的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n。 AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.d__1.MoveNext()\ r \ n ---從拋出異常的前一個位置的堆棧跟蹤結束---在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.d__2.MoveNext()\ r \ n ---結束堆棧跟蹤上一個拋出異常的位置--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n位於Microsoft的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n。 AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.d__6.MoveNext()\ r \ n --- s結尾從先前位置拋出異常的追蹤 - 拋出異常的情況--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)中返回\ r \ n \ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo。在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(狀態&下一步,作用域&作用域,對象&)Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext上下文)\ r \ n上拋出()\ r \ n狀態,Boolean & isCompleted)\ r \ n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)上的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() \ r \ n ---從之前位置拋出異常的堆棧跟蹤結束--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter處的\ r \ n。 HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.d__3.MoveNext()\ r \ n ---從以前位置拋出異常的堆棧跟蹤結束--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo。拋出()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在IdentityModel.AspNetCore.ScopeValidation.ScopeValidationMiddleware.d__3.MoveNext()\ r \ n ---結束堆棧跟蹤從以前的位置拋出異常--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務)\ r \ n在Microsoft .AspNetCore.Authentication.AuthenticationMiddleware
1.<Invoke>d__18.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d__18.MoveNext()\ r \ n ---拋出異常的前一個位置的堆棧跟蹤結束--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw( )\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationMiddleware.d__7.MoveNext()\ r \ n結束堆棧跟蹤從以前的位置拋出異常--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)\ System \ Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n \ Microsoft \ Asp \ \ r \ n ---從之前位置拋出異常的堆棧跟蹤結束--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter處的\ r \ n。 HandleNonSuccessAndDebuggerNotification(任務任務)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at Celo.Api.Middleware.Exceptions.ExceptionHandlingMiddleware.d__5.MoveNext()in D:\ a \ 1 \ s \ src \ Celo.Api \ Middleware \ Exceptions \ ExceptionHandlingMiddleware.cs:line 41「}]
我不知道這是如何失敗。
參考curl命令是:
curl -X PUT https://mydomain.co.nz/api/Account -H 'accept: application/json' -H 'authorization: Bearer eyJhb..' -H 'cache-control: no-cache' -F 'data={"isoncall" : true}'
和JavaScript調用是:
let xhr:XMLHttpRequest = new XMLHttpRequest();
xhr.onreadystatechange =() => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('200', xhr.response)
//resolve(<MyEntity>JSON.parse(xhr.response));
} else {
console.log('err', xhr.response)
//reject(xhr.response);
}
}
};
xhr.open('PUT', 'https://mydomain.co.nz/api/Account', true);
xhr.setRequestHeader("Accept", "application/json")
xhr.setRequestHeader("Authorization", this.authService.getAuth())
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=--------------------------33e4cd665cd7a003')
xhr.send(`--------------------------33e4cd665cd7a003
Content-Disposition: form-data; name="data"
{"isoncall" : true}
--------------------------33e4cd665cd7a003--`)
嘗試插入'\ r \ N'代替新線的車身數據'------------------------ --33e4cd665cd7a003 \ r \ n內容處理:表單數據; name =「data」\ r \ n \ r \ n {「isoncall」:true} \ r \ n -------------------------- 33e4cd665cd7a003 - \ r \ n' –
試過,但它沒有工作 xhr。send('-------------------------- 33e4cd665cd7a003 \ r \ nContent-Dispos ition:form-data; name =「data」\ r \ n \ r \ n {「isoncall」:true} \ r \ n -------------------------- 33e4cd665cd7a003 - \ r \ n ') 雖然感謝 –