HttpWebRequest Http Headers - c#

I'm using HttpWebRequest component and trying to POST data to server.
When I use browser I can trace that it sends this request
POST https://plus.google.com/_/socialgraph/mutate/modifymemberships/?_reqid=1950158&rt=j HTTP/1.1
Host: plus.google.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-Same-Domain: 1
Content-Type: application/x-www-form-urlencoded;charset=utf-8
Referer: https://plus.google.com/_/apps-static/_/js/home/b,s/rt=h/ver=OeD97kiQfn8.en./sv=1/am=!iPoVWvyH1UcKPT9bc1pNSZLcSj8oHAelto24gJorhwk/d=1/
Content-Length: 216
Cookie: PREF=ID=c42cff62752fc3e8:U=dae37ffa177e0689:FF=0:TB=2:LD=en:CR=2:TM=1244434765:LM=1311323210:DV=0SJoub_nzRUL:GM=1:S=EuE8opfqNl8PGGHI; NID=54=enDovqVn7CSHrPtZ2ZSPAt4PrE0ZZE-rWJawva2NiUWC1TzreG-sVdiSnRp7kolqcvMCGCIKt7agHKT6Hi2JZkV5qYmY_fUkxFjk6PUc7TrdNRAT7_9mRnvwOxeBOcng; rememberme=false; __utma=1.340991765.1324442547.1324723420.1324806933.8; __utmz=1.1324723420.7.7.utmcsr=google.com|utmccn=(referral)|utmcmd=referral|utmcct=/accounts/Logout2; OTZ=1044056_4_4_133320_8_385320; S=analytics-realtime-frontend=h23QCGj_I9O7FLG9cqDjkA:awfe=SnIt_U1wxuihQ1myCwuiZA:awfe-efe=lCXLSEuiUzX3HyaRuXVuRg:adwords-optimization=yCZeLrx1FD0uGQFYb07LTQ:adwords-common-ui=WfNukVX7rVpTuVtMlZ6IbA; S_awfe=tyb6VRVQyBHkv8nbfMNWRg; GMAIL_RTT=571; __utmc=1; HSID=AtaMO-oLk9-OMZkiv; SSID=AGt3gDSdd3Q98pA81; APISID=7xRgosYs7uOXzd0Y/AI4OXaHRTViR8oE8s; SAPISID=0HNI_ffNupNL-UfT/Ao6YbG5vkEashPdOM; ULS=EgYKBBICZW4Yy-Xb9wQ; SID=DQAAALMAAADk5OelxcPoQKO8FrSoOfOGoTHX4HeVfeJJQhtd19k07mJMUhJkiehF1FTzb7jNt34c80UGpyXEJT5FML373p6RR_5EQ3etVYjUXmgqHsdO7G-XuP_k_m798MDvd4AWkDX2vKVtqc5zA_olxB9UkJBLU_g2IM2vZ3Scp-eThZj8C3uZuzdN_DCpQeVjrU6a1kiIdQRsz9rUtxzoC0E22Ux4ba0QneaWBT73Ns14wh7fqZtYB-DRB2zcWkXXoNnV67U; FBS_VI_1324806859_0=ot_00Qg-hkQV7uhIEb0dTMT0_DD6aAI
Pragma: no-cache
Cache-Control: no-cache
a=%5B%5B%5B%225947b6d78a8231f3%22%5D%5D%5D&m=%5B%5B%5B%5Bnull%2Cnull%2C%22113690702042497323038%22%5D%2C%22Stephanie%20Parker%22%2C%5B%5D%5D%5D%5D&r=%5B%5B%5D%5D&at=AObGSAg06IXl5iYSceecKRc64xHVAB_GDw%3A1324806859000&
And when I use HttpWebRequester is sends following data which results in Code 400 rather then Code 200
POST https://plus.google.com/_/socialgraph/mutate/modifymemberships/?_reqid=1950158&rt=j HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
Accept-Language: en-gb,en;q=0.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
X-Same-Domain: 1
Content-Type: application/x-www-form-urlencoded;charset=utf-8
Referer: https://plus.google.com/_/apps-static/_/js/home/b,s/rt=h/ver=OeD97kiQfn8.en./sv=1/am=!iPoVWvyH1UcKPT9bc1pNSZLcSj8oHAelto24gJorhwk/d=1/
Pragma: no-cache
Cache-Control: no-cache
Host: plus.google.com
Cookie: ULS=EgYKBBICZW4Yp_7k9wQ; SID=DQAAALYAAAAZuUVe6hDQbW__dA1Itrcz8thm2Ncxk5YFMfpeiKyr6Pps1Pu0ajmReczMDHKGnDpk3yrx7WFy-5QMOf0Fu4WiPVdUWcU_cLNfD7FhKtqmSmOgFiffTkyj8dg0bsTeahhrKR7j75rjU_eXqGnQI2qcJHOyk1cLEkWNaWyLRGeYDrsfAh6uzx4AI-1GtKbwwrE7UHsn_sUFG1DuwI_Ct_g9C2llFVVyQruvFnlmRE1xDE_ORwvnBYCrGQYjyt1blg8; HSID=AbkExs5EzrPodpoKx; SSID=A2y_-xD7iOhW9Y2HF; APISID=73YQ7BugDgjmMSdd/AgFF1ClZc0z-fpPGM; SAPISID=mT6qzvCACzlvDrg-/AN1ajSgZCB0R9xOmX
Content-Length: 216
Connection: Keep-Alive
a=%5b%5b%5b%225947b6d78a8231f3%22%5d%5d%5d&m=%5b%5b%5b%5bnull%2cnull%2c%22113690702042497323038%22%5d%2c%22Stephanie%20Parker%22%2c%5b%5d%5d%5d%5d&r=%5b%5b%5d%5d&at=AObGSAg06IXl5iYSceecKRc64xHVAB_GDw%3a1324806859000&
So any idea why this to similar requests have 2 different server response?
BTW I did check cookies and they are fine. I'm just puzzled on why my request would not work.
UPDATE Request Code added
webRequest_ = (HttpWebRequest)HttpWebRequest.Create(Params.URL);
webRequest_.Method = "POST";
//webRequest_.Proxy = new WebProxy("http://plus.google.com", false);
webRequest_.UserAgent = Params.UserAgent;
webRequest_.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
webRequest_.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webRequest_.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webRequest_.KeepAlive = true;
webRequest_.Headers.Add("X-Same-Domain", "1");
webRequest_.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
webRequest_.Referer = "https://plus.google.com/_/apps-static/_/js/home/b,s/rt=h/ver=OeD97kiQfn8.en./sv=1/am=!iPoVWvyH1UcKPT9bc1pNSZLcSj8oHAelto24gJorhwk/d=1/";
webRequest_.CookieContainer = Params.Cookie;
webRequest_.Headers.Add("Pragma", "no-cache");
webRequest_.Headers.Add("Cache-Control", "no-cache");
var sp = webRequest_.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);
var parameters = new StringBuilder();
foreach (var key in Params.Params)
{
parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(key.ToString()),
HttpUtility.UrlEncode(Params.Params[key.ToString()]).Replace("+","%20"));
}
using (var writer = new StreamWriter(webRequest_.GetRequestStream()))
{
writer.Write(parameters.ToString());
}

After extensive testing I have figured out that problem is not in a header but rather in the data that gets posted, particularly in "at" parameter which is stands for session Id. After successfully generation session id it did work like a charm.

Related

Restsharp adds guid to body, how do I remove it?

My code is below, it is very simple. I was trying to match a postman call and it came pretty close, but when I made the call in postman it looks different, specifically restsharp adds a guid around the body that I saw in fiddler. The guide seems to break my calls, how do I remove it?
string url = "https://url/api/";
var client = new RestClient(url);
client.AddDefaultHeader("x-ads-dev", "Key");
var request = new RestRequest("telemetry", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", datapoint, ParameterType.RequestBody);
var response = await client.PostAsync(request);
And here is what Fiddler shows:
POST http://url/api/telemetry HTTP/1.1
Host: api.adsprism.com
x-ads-dev: key
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/107.3.0.0
Accept-Encoding: gzip, deflate, br
Content-Type: application/json; boundary="717cb9fd-684d-424e-bd0a-cf6b0bb11bac"
Content-Length: 398
--717cb9fd-684d-424e-bd0a-cf6b0bb11bac
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="application/json"
[{"entityID":2123,"locationID":33,"data":[{"dateTime":"2020-08-03 23:05:00","reading":0,"flags":0,"quality":15,"ignore":false},{"dateTime":"2020-09-27 03:10:00","reading":0,"flags":0,"quality":15,"ignore":false}]}]
--717cb9fd-684d-424e-bd0a-cf6b0bb11bac--
The --717cb9fd-684d-424e-bd0a-cf6b0bb11bac-- guid is not there in the fiddler postman request and it seems to be causing a bad request error from restsharp. So how do I make restsharp not generate it?
EDIT: Added postman fiddler to show the difference.
POST http://api.url/api/telemetry HTTP/1.1
x-ads-dev: key
Content-Type: application/json
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Postman-Token: 5f186e6d-cad0-482c-b7b8-a50559f24b87
Host: api.adsprism.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 513
[
{
"entityID": 2123,
"locationID": 33,
"data": [
{
"dateTime": "2020-08-03 23:05:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
},
{
"dateTime": "2020-09-27 03:10:00",
"reading": 0,
"flags": 0,
"quality": 0,
"ignore": true
}
]
}
]
Reading the docs can help.
const string url = "https://url/api/";
var client = new RestClient(url);
client.AddDefaultHeader("x-ads-dev", "Key");
var request = new RestRequest("telemetry", Method.Post);
request.AddStringBody(datapoint, DataType.Json);
var response = await client.PostAsync(request);

Multipart POST to the Joplin REST API using C# and Flurl

I am currently working on a console app to import data into Joplin for Windows 10, using C# and Flurl.
Joplin's API description can be found here.
I am trying to create a new resource in Joplin for a file on my system, so it can be attached to a Joplin note.
With CURL I can create the resource using command:
curl -F "data=#c:\\temp\\Test.pptx" -F "props={\"title\":\"my resource title\"}" http://localhost:41184/resources?token=MyToken
(note: it only works with "data=#c:\temp\Test.pptx", NOT with "data=c:\temp\Test.pptx")
When I try this with Flurl in c# I get a 400 response from Joplin, in the log I find:
Error: Resource cannot be created without a file
at Api.action_resources (C:\Program Files\Joplin\resources\app.asar\lib\services\rest\Api.js:351:37)
at Api.route (C:\Program Files\Joplin\resources\app.asar\lib\services\rest\Api.js:140:42)
at execRequest (C:\Program Files\Joplin\resources\app.asar\lib\ClipperServer.js:157:39)
at C:\Program Files\Joplin\resources\app.asar\lib\ClipperServer.js:185:8
at C:\Program Files\Joplin\resources\app.asar\node_modules\multiparty\index.js:136:9
at C:\Program Files\Joplin\resources\app.asar\node_modules\multiparty\index.js:115:9
at processTicksAndRejections (internal/process/task_queues.js:75:11)"
I have tried this so far:
try
{
var url = BaseUrl
.WithHeader("User_Agent", browserUserAgent)
.AppendPathSegment("resources")
.SetQueryParam("token", Token);
using (var fs = new FileStream("c:\\temp\\Test.pptx", FileMode.Open, FileAccess.Read))
{
var resource = url.PostMultipartAsync(mp => mp
.AddJson("props", new { title = "test title" })
.AddFile("data", fs, "Test.pptx", "application/octet-stream")
)
.ReceiveJson<JoplinResource>()
.Result;
}
}
and:
try
{
var url = BaseUrl
.WithHeader("User_Agent", browserUserAgent)
.AppendPathSegment("resources")
.SetQueryParam("token", Token);
var resource = url.PostMultipartAsync(mp => mp
.AddJson("props", new { title = "test title" })
.AddFile("data", "c:\\temp\\Test.pptx")
)
.ReceiveJson<JoplinResource>()
.Result;
}
I hooked up fiddler to see what is the difference between my application and CURL.
Curl:
POST http://127.0.0.1:41184/resources?token=MyToken HTTP/1.1
Host: 127.0.0.1:41184
User-Agent: curl/7.70.0
Accept: */*
Connection: Keep-Alive
Content-Length: 33648
Content-Type: multipart/form-data; boundary=------------------------91ab181cbb0247ba
--------------------------91ab181cbb0247ba
Content-Disposition: form-data; name="props"
{"title":"my resource title"}
--------------------------91ab181cbb0247ba
Content-Disposition: form-data; name="data"; filename="Test.pptx"
Content-Type: application/octet-stream
...
My Console app:
POST http://localhost:41184/resources?token=MyToken HTTP/1.1
User_Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
Content-Type: multipart/form-data; boundary="f603841b-5c32-4e77-985a-69c2ffb6eed0"
Host: localhost:41184
Content-Length: 33612
Expect: 100-continue
Accept-Encoding: gzip, deflate
--f603841b-5c32-4e77-985a-69c2ffb6eed0
Content-Disposition: form-data; name=props
{"title":"My Resource"}
--f603841b-5c32-4e77-985a-69c2ffb6eed0
Content-Disposition: form-data; name=data; filename=Test.pptx; filename*=utf-8''Test.pptx
...
NOTE the differences:
props and data are in quotes when using CURL, not with FLURL
FLURL sends a second file name: filename*=utf-8''Test.pptx
How do I get this to work properly?
The issue was in the missing quotes for the "data" and "props":
try
{
var url = BaseUrl
.WithHeader("User_Agent", browserUserAgent)
.AppendPathSegment("resources")
.SetQueryParam("token", Token);
var resource = url.PostMultipartAsync(mp => mp
.AddJson("\"props\"", new { title = "My Resource" })
.AddFile("\"data\"", "c:\\temp\\Test.pptx")
)
.ReceiveJson<JoplinResource>()
.Result;
}
Raw request header is now:
POST http://localhost:41184/resources?token=MyToken HTTP/1.1
User_Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
Content-Type: multipart/form-data; boundary="c6b2377a-1240-4ae3-872f-fa24b643d3e0"
Host: localhost:41184
Content-Length: 33616
Expect: 100-continue
Accept-Encoding: gzip, deflate
--c6b2377a-1240-4ae3-872f-fa24b643d3e0
Content-Disposition: form-data; name="props"
{"title":"My Resource"}
--c6b2377a-1240-4ae3-872f-fa24b643d3e0
Content-Disposition: form-data; name="data"; filename=Test.pptx; filename*=utf-8''Test.pptx
...
And the Joplin REST service creates a new resource...

C# Http WebRequest JSON

I'm new to WebRequests and I tried this:
string json = #"{""Gebruikersnaam"":""user"",""Wachtwoord"":""pass"",""IngelogdBlijven"":true}";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pontessg.magister.net/api/sessies");
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = json.Length;
Console.WriteLine(json.Length);
Console.WriteLine(json);
Console.Read();
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
My code is keep returning me ERROR (400) and I don't really know why I think I forgot something, but I don't know what
(The Headers)
Request URL:https://pontessg.magister.net/api/sessies
Request Method:POST
Status Code:200 OK
Remote Address:[...]
Response Headers
view parsed
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 76
Content-Type: application/json; charset=utf-8
Expires: -1
Date: Sat, 25 Feb 2017 21:45:33 GMT
X-Frame-Options: DENY
Strict-Transport-Security: max-age=[...]
Request Headers
view parsed
POST /api/sessies HTTP/1.1
Host: pontessg.magister.net
Connection: keep-alive
Content-Length: 88
Accept: application/json, text/plain, */*
X-API-Client-ID: 12D8
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Origin: https://pontessg.magister.net
Content-Type: application/json;charset=UTF-8
DNT: 1
Referer: https://pontessg.magister.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: SESSION_ID=[...]; M6UserName=user; username=pass; Magister.UserName=user; [...]
Request Payload
{"Gebruikersnaam":"user","Wachtwoord":"pass","IngelogdBlijven":true}
When I try the JS code below in the console of my browser, it returns 200 OK
$.ajax({
url: "https://pontessg.magister.net/api/sessies",
type: "POST",
data: "{\"Gebruikersnaam\":\"user\",\"Wachtwoord\":\"pass\",\"IngelogdBlijven\":true}",
contentType: "application/json;charset=utf-8",
success: action_Succeeded,
error: action_Failed
});
function action_Succeeded(r) {
console.log("succes");
}
function action_Failed(r1, r2, r3) {
alert("fail");
}
If someone could help me with this, I would like that

C# .NET - HttpWebRequest - System.Net.WebException - Too many automatic redirections were attempted

I have a problem with HttpWebRequest class.
I am trying to get source code of website:
http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983
but I am always getting an error:
System.Net.WebException occurred
HResult=-2146233079
Message=Too many automatic redirections were attempted.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at ProjectName.ClassName.MethodName(String urlAddress)
InnerException:
That is my code:
Uri uri = new Uri(#"http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
I have used a Fiddler Web Debugger tool to compare Firefox request with my C# .NET request, but still have no answer.
Firefox:
GET http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983 HTTP/1.1
Host: www.filmweb.pl
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate
Content-Type: text/html;charset=UTF-8
Content-Language: pl-PL
Transfer-Encoding: chunked
Date: Wed, 07 Oct 2015 13:36:31 GMT
X-Cache: HIT from blade110.non.3dart.com
X-Cache-Hits: 116
Server: Apache
C# .NET:
GET http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci:+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983 HTTP/1.1
Host: www.filmweb.pl
Connection: Keep-Alive
HTTP/1.1 301 Moved Permanently
Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate
Content-Type: text/html;charset=UTF-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Language: pl-PL
Location: /film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983
Content-Length: 0
Accept-Ranges: bytes
Date: Wed, 07 Oct 2015 13:34:51 GMT
X-Cache: MISS from blade712.non.3dart.com
Server: Apache
I have read other posts and update my code by different things, eg.
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.TransferEncoding = "gzip, deflate";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0";
request.Referer = "http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 250;
request.Proxy = null;
request.UseDefaultCredentials = true;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
but nothing works :-/
Can anybody help me with this problem?
You need to have the initial cookies when the website load before you fetch a deep-link.
The following code works for me:
// cookies
CookieContainer cookieContainer = new CookieContainer();
// make one call to the root of the website
// to get the cookies set
Uri uri = new Uri(#"http://www.filmweb.pl");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookieContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using(var s = response.GetResponseStream())
{
using(var sr = new StreamReader(s))
{
// linqpad
sr.ReadToEnd().Dump(); // to check for errors
}
}
// we have cookies now
// do the deep link fetch
uri = new Uri(#"http://www.filmweb.pl/film/Igrzyska+%C5%9Bmierci%3A+Kosog%C5%82os.+Cz%C4%99%C5%9B%C4%87+1-2014-626983");
request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookieContainer;
response = (HttpWebResponse)request.GetResponse();
//store the result
using(var f = File.Create("C:\\temp\\pl.txt"))
{
response.GetResponseStream().CopyTo(f);
}
Make sure that if you scrape a website that you adhere to their license and usage policies. Don't do anything that goes beyond fair use or against any copy-righted materials.

PostAsJsonAsync POST variables don't arrive at Flight PHP REST server

I have a Flight PHP REST server set up.
At 1 end point it expects POST data and based on one of the POST data it retrieves some data from the database and returns it as JSON.
When I use the Postman REST extension in Chrome I see the correct result. But when I do the call using my C# application the returned json is null because the $_POST seems empty.
This is my Flight index.php:
Flight::route('POST /getText', function(){
// Create a new report class:
$reportText = new ReportText;
$theText = $reportText->ParsePost($_POST);
if ($theText == null)
{
echo null;
}
else
{
echo json_encode($theText);
}
});
This is my ParsePost:
public function ParsePost($PostDictionary)
{
$textArray = null;
foreach ($PostDictionary as $key => $value)
{
if (!empty($value))
{
list($tmp, $id) = explode("_", $key);
$text = $this->geFooWithText($id);
$textArray[$key] = "bar";
}
}
return $textArray;
}
This is my C# part:
private static async Task RunAsyncPost(string requestUri)
{
using (var client = new HttpClient())
{
// Send HTTP requests
client.BaseAddress = new Uri("myUrl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// HTTP POST
var response = await client.PostAsJsonAsync(requestUri, new { question_8 = "foo", question_9 = "bar" });
response.EnsureSuccessStatusCode(); // Throw if not a success code.
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(json))
{
throw new ArgumentNullException("json", #"Response from the server is null");
}
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (var kvp in dictionary)
{
Debug.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
}
}
catch (HttpRequestException e)
{
// Handle exception.
Debug.WriteLine(e.ToString());
throw;
}
}
}
This is the response from Postman:
{
"question_8": "foo",
"question_9": "bar"
}
It seems I'm missing something in my call using C#.
[Update]
In this post (Unable to do a HTTP POST to a REST service passing json through C#) the same problem seems to appear.
Using Fiddler was suggested.
Using Postman and x-www-form-urlencoded:
POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 29
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
question_8=foo&question_9=bar
Using Postman and form-data:
POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 244
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvb4wmaP4KooT6TFu
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_8"
foo
------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_9"
bar
------WebKitFormBoundaryvb4wmaP4KooT6TFu--
Using my C#-application:
POST http://myHost/api/getText/ HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: myHost
Content-Length: 50
Expect: 100-continue
Connection: Keep-Alive
{"question_8":"foo","question_9":"bar"}
The C# application is sending it clearly in a different way.
I've found the problem.
Because I use client.PostAsJsonAsync() the POST data is send as json, as you can see in Fiddler.
PHP doesn't expects the POST data to be in json format.
To read that data I need to use $data = file_get_contents('php://input'); in my PHP file.
Now I have my keys and values and can I continue. It looks like PHP needs a $_JSON variable ;)

Categories