2017-07-31 185 views
0

我有兩個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--`) 
+0

嘗試插入'\ r \ N'代替新線的車身數據'------------------------ --33e4cd665cd7a003 \ r \ n內容處理:表單數據; name =「data」\ r \ n \ r \ n {「isoncall」:true} \ r \ n -------------------------- 33e4cd665cd7a003 - \ r \ n' –

+0

試過,但它沒有工作 xhr。send('-------------------------- 33e4cd665cd7a003 \ r \ nContent-Dispos ition:form-data; name =「data」\ r \ n \ r \ n {「isoncall」:true} \ r \ n -------------------------- 33e4cd665cd7a003 - \ r \ n ') 雖然感謝 –

回答

1

兩個請求之間有着明顯的區別是,在你的XHR請求,你不這樣做前綴根據需要使用兩個破折號填寫請求正文中的邊界。

如果你看看CURL生成的請求,你會看到兩個邊界有兩個額外的破折號(與頭中的邊界相比),以及兩個額外的破折號後綴到身體。您的JavaScript代碼中沒有前綴。現在

,話雖如此,我可以建議兩個可能的解決方案:

  1. 解決當前的代碼 - 這可能是一個快速解決方案。

    如上所述,您需要將附加 - 前綴添加到正文中的邊界。 其次,我會避免使用多行字符串,並使用\ n作爲行分隔符。
    這將導致以下的javascript:

    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\n' + 
        'Content-Disposition: form-data; name="data"\n\n' + 
        '{"isoncall" : true}\n\n' + 
        '----------------------------33e4cd665cd7a003--'); 
    
  2. 你也可以切換到XHR FormData API,並避免這一切手動處理。

    var formData = new FormData(); 
    formData.append("data", '{"isoncall" : true}'); 
    xhr.open('PUT', 'https://mydomain.co.nz/api/Account', true); 
    xhr.setRequestHeader("Accept", "application/json"); 
    xhr.setRequestHeader("Authorization", this.authService.getAuth()); 
    xhr.send(formData); 
    
+0

謝謝 - 第二個工作。我必須等待21小時才能獎勵你的賞金,謝謝 –