Sending multipart/mixed http request to an asp.net web api controller - asp.net

I'd like to send a multipart/mixed http request ($batch) to an asp.net web api controller, as follows:
--batch_fb0c-6847-2810
Content-Type: multipart/mixed; boundary=changeset_08b4-5b66-0d79
--changeset_08b4-5b66-0d79
Content-Type: application/http
Content-Transfer-Encoding: binary
POST People HTTP/1.1
Content-Id: 1
Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
DataServiceVersion: 1.0
Content-Type: application/json
MaxDataServiceVersion: 3.0
{"Id":1,"Task":"1","DueDate":"2013-02- 21T11:34:06.247","Completed":false,"University":1,"Degree":1}
--changeset_08b4-5b66-0d79
Content-Type: application/http
Content-Transfer-Encoding: binary
POST People HTTP/1.1
Content-Id: 2
Accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1
DataServiceVersion: 1.0
Content-Type: application/json
MaxDataServiceVersion: 3.0
{"Id":4,"Task":"2","DueDate":"2013-02-21T11:34:06.247","Completed":false,"University":4,"Degree":4}
--changeset_08b4-5b66-0d79--
--batch_fb0c-6847-2810--
The problem is my request is not routed to proper action in my controller, is there any solution out there to accomplish this issue?!

The previous answer is outdated. Batch support has been added to the Web API project, complete documentation is here:
https://aspnetwebstack.codeplex.com/wikipage?title=Web+API+Request+Batching

Related

HTTP/1.1 401 Authorization Required Error - Twitter API

Getting Error 401 while sending the below request for stream api .
however when i just try using curl with authorization header it works properly .
Please advice what m doing wrong.
REQUEST
GET /1.1/statuses/filter.json HTTP/1.1
Accept: /
User-Agent: Qapla
Content-Type: application/x-www-form-urlencoded
Authorization: https://stream.twitter.com/1.1/statuses/sample.json?language=en&oauth_consumer_key=GuYuxjwY&oauth_nonce=u4a3R_n&oauth_signature_method=HMAC-SHA1&oauth_timestamp=153693&oauth_token=103405936-vfmrGkclVE8siUIF&oauth_version=1.0&oauth_signature=R2qVlT2aLVgi8SMA%3D
Host: stream.twitter.com
ERROR RESPONSE
HTTP/1.1
401
Authorization
Required
cache-control:
must-revalidate,no-cache,no-store
connection:
close
content-length:
266
content-type:
text/html
date:
Fri,
14
Sep
2018
15:19:31
GMT
server:
tsa
www-authenticate:
Basic
realm=“Firehose”
x-connection-hash:
cd3e38a2d55e0a8831776b96d862d3a2
The authorization header is invalid , it should not be a url , rather it should be some has kind of value , in case u have access to the API , you will find it in the API console

Upload image from URL through Drive API

I'm using Postman to learn how to use the Google Drive API.
I'm trying to upload an API to an image that is in a URL. I'm using the multipart upload described in this documentation link, but the example is not clear to me, I can upload a file with the metadata I'm passing, but the image of the URL I've placed is not being sent.
Please help me understand what I'm doing wrong. Below is the code for my request.
POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Content-length: *
Authorization: [myToken]
Content-Type: multipart/related; boundary=test
Cache-Control: no-cache
Postman-Token: [postmanToken]
--test
Content-Type: application/json; charset=UTF-8
{
"name": "imageTest",
"parents": ["1Ij0ZR8yrubkHQaT6tSQNgK6AhW4gbP49"]
}
--test
Content-Type: image/*
https://download.shutterstock.com/gatekeeper/W3siZSI6MTUzNjcwMzQ0NSwiayI6InBob3RvLzEwMDcwNjYwMjMvaHVnZS5qcGciLCJtIjoxLCJkIjoic2h1dHRlcnN0b2NrLW1lZGlhIn0sInR3UkJ0bDZNYUJNUXJ2azZlaE9MbTZlT2VBbyJd/shutterstock_1007066023.jpg
--test--
I doubt that you will be able to use Postman to upload a file. Its going to have to read the file first and then send the file data in chunks.
All of the examples google has for this is using a programming language there is no samples of doing it directly with HTTP calls file upload
If it did work you would be looking at something like this. You are still going to need some language to read the file contents and add it to this.
POST / HTTP/1.1
[[ Less interesting headers ... ]]
Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
Content-Length: 834
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text1"
text default
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text2"
aωb
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain
Content of a.txt.
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html
<!DOCTYPE html><title>Content of a.html.</title>
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file3"; filename="binary"
Content-Type: application/octet-stream
aωb
-----------------------------735323031399963166993862150--

Spotify Web API authentication getting token returns invalid redirect uri

I'm trying to get a token from Spotify using the Web API:
My raw request:
POST https://accounts.spotify.com/api/token HTTP/1.1
Authorization: Basic [authsecret]
Content-Type: application/x-www-form-urlencoded
Host: accounts.spotify.com
Content-Length: 490
Expect: 100-continue
Connection: Keep-Alive
grant_type=authorization_code&code=[code]&redirect_url=http%3A%2F%2Flocalhost%3A53575%2FSpotifyLogin.aspx
Where [authsecret] is replaced by the required string (this was broken and gave me a different error, but now I fixed it) and [code] is replaced by the code returned a step earlier by Spotify.
I get the following response:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 22 Jun 2017 13:50:33 GMT
Content-Type: application/json
Content-Length: 68
Connection: keep-alive
Keep-Alive: timeout=600
{"error":"invalid_grant","error_description":"Invalid redirect URI"}
I must be doing something wrong, but I can't figure it out. Can anyone help me?
I've figured it out. Issue is the parameter name redirect_url. It should be redirect_uri. Small mistake but breaks the request as it should.

Batch request in Google Calendar V3 REST API

How to get/add/update Google Calendar Events using batch request through V3 REST API? I've tried and but not works. According to the docs (https://developers.google.com/google-apps/calendar/batch) it should be possible to send a batch request by posting a multipart/mixed content type message to the API. An example of a working HTTP POST would be great.
Thanks,
Riyaz
I found that the outer URL must be "https://www.googleapis.com/batch/calendar/v3/"
and the boundary url must be "/calendar/v3/calendars/{calendarID}/events"
The full HTTP request looks like:
POST /batch/calendar/v3 HTTP/1.1
Authorization: /*Auth token*/
Host: host
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length
--batch_foobarbaz
Content-ID: 1
GET /calendar/v3/calendars/{calendarID1}/events
--batch_foobarbaz
Content-ID: 2
GET /calendar/v3/calendars/{calendarID2}/events
--batch_foobarbaz--
The following batch request, gets the eventId1, updates the eventId2 and creates a new event under calender that is identified with calendarId.
POST /batch HTTP/1.1
Authorization: /*Auth token*/
Host: host
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:x#barnyard.example.com>
GET /calendar/v3/calendars/calendarId/events/eventId1
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:x#barnyard.example.com>
PUT /calendar/v3/calendars/calendarId/events/eventId2
Content-Type: application/json
Content-Length: part_content_length
{{ body }}
--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:x#barnyard.example.com>
POST /calendar/v3/calendars/calendarId/events
Content-Type: application/json
Content-Length: part_content_length
{{ body }}
--batch_foobarbaz--
The endpoint is
https://www.googleapis.com/batch
That works for me when I do Calendar batch requests. One problem I had was with my last boundary token I didn't have the -- after it. So each token starts with -- and the last one has -- at the end. As #Burcu Dogan's example shows.

Why does my Twitter OAuth API call to update status fail, but other calls work?

This is the raw HTTP call that I make to verify authentication. It returns the expected response:
GET /1/account/verify_credentials.xml HTTP/1.1
Authorization: OAuth oauth_token="12556442-pndSo1mf2i1ToPSbAyLH4qBBDHmtyutjbvMLckGER",oauth_consumer_key="ih75ityikrTdIwB9kQ",oauth_nonce="6wIbdfxL",oauth_signature_method="HMAC-SHA1",oauth_signature="7DUW5TLtntryndfhU5dSXARg%3D",oauth_version="1.0",oauth_timestamp="1267805254"
Host: api.twitter.com
This is a call that I (try) to make, which is intended to update the users status:
POST /1/statuses/update.xml HTTP/1.1
Authorization: OAuth oauth_token="1252356242-pndSo1mf2i1ToPSfghfghfQoMLckGER",oauth_consumer_key="ih75i83BXdfhnfghnfgQ",oauth_nonce="CJ9dfgXs",oauth_signature_method="HMAC-SHA1",oauth_signature="bSD7aXUdfghdfghfghfghoU%3D",oauth_version="1.0",oauth_timestamp="1267235407"
Content-Type: application/x-www-form-urlencoded
Host: api.twitter.com
Content-Length: 11
Connection: Keep-Alive
status=blah
The response that I get back from twitter for this second request is as follows:
HTTP/1.1 401 Unauthorized
Date: Fri, 05 Mar 2010 16:17:18 GMT
Server: hi
Status: 401 Unauthorized
WWW-Authenticate: Basic realm="Twitter API"
Content-Type: application/xml; charset=utf-8
Content-Length: 135
Cache-Control: no-cache, max-age=1800
Set-Cookie: guest_id=12672352252251; path=/
Set-Cookie: _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGsdgsdhdrhvdrthvdthd0%250ANDdkZTEyZjczZTY3ZGE4YmQ5IgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--0eb657ba0esdrvthdtdtgcdrtgc0ece8f1460; domain=.twitter.com; path=/
Expires: Fri, 05 Mar 2010 16:47:17 GMT
Vary: Accept-Encoding
Connection: close
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/1/statuses/update.xml</request>
<error>Incorrect signature</error>
</hash>
Any idea what could be going wrong?
Note, the OAuth tokens and stuff have been scrambled of course.
Solved:
Even though I had to make this call a POST request, the actual parameters couldn't be in the POST body. I put the status variable in the query string on the request and it worked fine.
i got similar problem ...401 unauthorized ...using php
i was following the examples in
http://www.snipe.net/2009/07/writing-your-first-twitter-application-with-oauth/
until i discovered the parameters 'POST' and $data
was swapped in the line
$content = $to->OAuthRequest('https://twitter.com/statuses/update.xml','POST',$data);
maybe it is your case too?
maybe not anyway i found that tutorial very useful
cheer
POST parameters are fed into the hash that produces the signature, so you have to make sure they are included when you calculate it.

Resources