jonsamwell / angular-http-batcher

Enables HTTP batch request with AngularJS
MIT License
96 stars 28 forks source link

Not able to call Web API 2 Batch Endpoint #36

Closed venkatesh-bhupathi closed 8 years ago

venkatesh-bhupathi commented 8 years ago

I am trying to use http batcher sample code to call batch endpoint on ASP .Net Web API 2, but not able to make the batch calls. I am able to call the same batch endpoint with .Net client code without any issues. Following is the fiddler trace when calling through http batcher,

Request:

OPTIONS http://localhost:84/api/batch HTTP/1.1 Host: devx.microsoft-tst.com:84 Connection: keep-alive Access-Control-Request-Method: POST Origin: http://localhost:2019 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 Access-Control-Request-Headers: accept, content-type Accept: / Referer: http://localhost:2019/Home/Index Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8

Response:

HTTP/1.1 400 Bad Request Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 05 Jul 2016 14:44:43 GMT Content-Length: 68

{"Message":"The batch request must have a \"Content-Type\" header."}

Following is the fiddler trace for .Net client,

POST http://localhost:84/api/batch HTTP/1.1 Content-Type: multipart/mixed; boundary="batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c" Host: localhost:84 Content-Length: 857 Expect: 100-continue Connection: Keep-Alive

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

GET /api/Customers HTTP/1.1 Host: localhost:84

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

POST /api/Customers HTTP/1.1 Host: localhost:84 Content-Type: application/json; charset=utf-8

{"Id":10,"Name":"Name 10"} --batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

PUT /api/Customers/100 HTTP/1.1 Host: localhost:84 Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Peter"} --batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

DELETE /api/Customers/100 HTTP/1.1 Host: localhost:84

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c--

Response

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 795 Content-Type: multipart/mixed; boundary="1c3e08bd-7545-4b3a-bd0d-b291a436a7fb" Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 05 Jul 2016 12:10:19 GMT

--1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8

[{"Id":100,"Name":"Venkatesh"}] --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 201 Created Location: http://localhost:84/api/Customers Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Venkatesh"} --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Venkatesh"} --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 204 No Content

--1c3e08bd-7545-4b3a-bd0d-b291a436a7fb--

jonsamwell commented 8 years ago

Looks like you need to setup CORS. The first call is an option request. I'll dig out the CORS setup code for .net when I'm back home

On 00:53, Wed, 6 Jul 2016 venkatesh-bhupathi, notifications@github.com wrote:

I am trying to use http batcher sample code to call batch endpoint on ASP .Net Web API 2, but not able to make the batch calls. I am able to call the same batch endpoint with .Net client code without any issues. Following is the fiddler trace when calling through http batcher,

Request:

OPTIONS http://localhost:84/api/batch HTTP/1.1 Host: devx.microsoft-tst.com:84 Connection: keep-alive Access-Control-Request-Method: POST Origin: http://localhost:2019 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 Access-Control-Request-Headers: accept, content-type Accept: / Referer: http://localhost:2019/Home/Index Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8

Response:

HTTP/1.1 400 Bad Request Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 05 Jul 2016 14:44:43 GMT Content-Length: 68

{"Message":"The batch request must have a \"Content-Type\" header."}

Following is the fiddler trace for .Net client,

POST http://localhost:84/api/batch HTTP/1.1 Content-Type: multipart/mixed; boundary="batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c" Host: localhost:84 Content-Length: 857 Expect: 100-continue Connection: Keep-Alive

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

GET /api/Customers HTTP/1.1 Host: localhost:84

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

POST /api/Customers HTTP/1.1 Host: localhost:84 Content-Type: application/json; charset=utf-8

{"Id":10,"Name":"Name 10"} --batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

PUT /api/Customers/100 HTTP/1.1 Host: localhost:84 Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Peter"} --batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c Content-Type: application/http; msgtype=request

DELETE /api/Customers/100 HTTP/1.1 Host: localhost:84

--batch_75e852a8-ae17-4cf7-a601-1e3dbfd47f5c--

Response

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 795 Content-Type: multipart/mixed; boundary="1c3e08bd-7545-4b3a-bd0d-b291a436a7fb" Expires: -1 Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 05 Jul 2016 12:10:19 GMT

--1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8

[{"Id":100,"Name":"Venkatesh"}] --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 201 Created Location: http://localhost:84/api/Customers Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Venkatesh"} --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8

{"Id":100,"Name":"Venkatesh"} --1c3e08bd-7545-4b3a-bd0d-b291a436a7fb Content-Type: application/http; msgtype=response

HTTP/1.1 204 No Content

--1c3e08bd-7545-4b3a-bd0d-b291a436a7fb--

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jonsamwell/angular-http-batcher/issues/36, or mute the thread https://github.com/notifications/unsubscribe/ADhfoV-0wIM0f5pS3xl0HtmQE32flclDks5qSm_ZgaJpZM4JFM7J .

jonsamwell commented 8 years ago

You will first need to enable CORS (needed for the angular client to talk to your service across different domains).

// Set global CORS policy which enable CORS on the batch route var cors = new EnableCorsAttribute("*", "*", "*", "Location") { PreflightMaxAge = 28800 }; config.EnableCors(cors);

Then enable HTTP batching.

httpServer.Configuration.Routes.MapHttpBatchRoute(routeName: "batch", routeTemplate: "batch", batchHandler: new CompressingHttpBatchHandler(httpServer) { ExecutionOrder = BatchExecutionOrder.NonSequential });