I've got a Silverlight app that talks to a REST web service using the WebClient class. It runs fine in IE 8 and Chrome 5, but the web service call fails in Firefox 3.
I've narrowed down the problem: Firefox 3 is changing the Accept header of my HTTP request. here's my simplified code:
// Use the ClientHttp stack.
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
// Call the web service.
var webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringAsync(someUrl);
Using Fiddler to investigate the data on the pipes, the request has its header replaced:
GET /1/36497f32-1acd-4f4e-a946-622b3f20dfa5/Content/GetAllTextContentsForUser/0 HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Host: localhost:88
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Notice the 2nd line, "Accept" -- it's been replaced with text/html, xml, and other formats. Not what I'm looking for -- I absolutely need JSON coming back.
Is there a way to prevent Firefox from modifying my Accept header?
Turns out the Accept header is being overwritten in all browsers. IE and Chrome overwrite the Accept header with the value of "*/*" (star slash star). Firefox overwrites it with some strange value that specifies XML, among other things.
The answer to this problem is you cannot expect to have your Accept header preserved. The browser will overwrite the Accept header, even if you use ClientHttp.
Related
I am testing my proxy that simply sends a client's request to a proxied server and returns a response back. The current realization requires that the client must send the fully prepared valid request to the proxy (the Host header value must match with a DNS of the predefined in source code proxied server).
Heres my custom request to the proxy that proxies a www.example.com:
But the result request that ARC sends to the localhost is:
GET / HTTP/1.1
Host: localhost:1234
connection: close
then it is sent to the www.example.com but the Host header is invalid for it so 404 is returned as a result.
I just noticed that this refers to the old version of ARC for Chrome. Support for Chrome apps is scheduled to end soon the the app is no longer supported. Instead, please, install desktop client from https://install.advancedrestclient.com/
To move your data from one app to another follow instructions from https://docs.advancedrestclient.com/moving-from-chrome-application-to-desktop-client
I have Angular application A, that will open by other application B, while opening application by B, they will send one attribute in the request header, how can i accesses, that custom header in my angular application when its opening, the custom header i can seen tcpdump.
Below is the header, i want accesses 'acbd' value
Connection: keep-alive
Referer:
User-Agent: Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; HTC Desire Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Accept-Encoding: gzip,deflate
Accept-Language: en-US
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
Cookie: NG_TRANSLATE_LANG_KEY=%22en%22
**abcd: 13223223**
Http headers are not accesible via Javascript (only http-referer and http-user-agent via object properties, and http-cookie). If you need to pass some value from one application to another, you can use cookies and retrieve value parsing document.cookie variable.
Edit: Headers can be accessed via XmlHttpRequest object when using Ajax requests, but only restricted to simple response headers, and additional restrictions by CORS if your request is cross-domain. Some cookies marked as Http-only cannot be accessed via Javascript.
This error has perplexed me. I have been working on this application for a year now and one of the most original parts of it just started failing for no apparent reason. I haven't made any configuration changes or changes in and around the area which is failing, so it is baffling me.
Requirement
In my SPA, click on a button and the JSON model associated to that button is sent to the server to be saved.
Implementation
return $resource('/api/audits/:auditId/auditAnswers', { auditId: auditId, auditAnswer: auditAnswer }).save(auditAnswer);
This in turn is handled by WebAPI 2.0
[Route("api/audits/{auditId}/auditAnswers")]
[HttpPost]
public ServiceResult SaveAuditAnswerByAudit(HttpRequestMessage request, Guid auditId, [FromBody]AuditAnswerModel auditAnswer)
{ ... }
Issue
This worked for a good year without issue. Now when I test in Firefox 46.0.1 hosted in IIS Express, only about 1 in 5 attempts actually reaches the WebAPI method. No other post seems to reach WebAPI at all.
The error occurs as soon as I attempt the post, so it does not seem like it's timing out, either.
Firefox reports
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 348
Content-Type application/json;charset=utf-8
Host localhost:61032
Referer http://localhost:61032/QAT/Audits/f3fa490d-6324-4227-b8fa-a6be4d33dd82
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
Also, no HTTP status code is reported.
The error Callback for $resource is called and the response object contains:
data null
status 0
statusText ""
I have come across this once already and fixed it by not posting the entire JSON to WebAPI, but there are times I need to do this and consider it the correct way to do so.
Thinking it may be browser related I have restarted the browser, computer, cleared cached, and none of these methods resolved the issue. However, using a different browser such as Chrome does not encounter this issue (yet).
The fact that it works "eventually" (after about 5 attempt) is most concerning to me.
Question
Is this a known issue? Is it my fault? Is there something I can do to fix it?
On server side is there any way to detect that particular request to API came from mobile device (from mobile app)?
I know about user agent sniffing but I dont like this aproach from few enough reasons not to implement it.
I also know I could add some flag to request when it comes from my mobile app, but this seems bit dirty as well.
Are there actually any 'proper' ways to do it?
I guess it doesn't change much but my backend is in node.js.
Greetings, thanks!
Tom
The general answer is no. You get a header / message from a device. All you know about the device is in the header and the device can write what it wants in it. If you are talking about http requests (which is indicated by agent lookup) you can look at a header here:
All you can do "reliable" is to look for the user agent. In my case it is Mozilla Firefox on Linux. But I could fake it if I want.
Host: somesite.org
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.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
Referer: http://somesite.org/index.php?page=2
Cookie: rteStatus=rte;
Cache-Control: max-age=0
Maybe you can get some informations from the referer if it is some chromium-mobile site or you can have a look at Accept and Accept-Enconding, maybe some mobile browsers accept different stuff. But there is no reliable way to determine the device but by its user Agent via header.
An other approach is to look if the request comes from an IP known as 3G or 4G pool. But this would just work if the requests is not coming via WLAN / WIFI. And I am not sure if a list of 3G / 4G IP address pools exists.
You can use UserAgent string for detecting. Below code in C#.
public bool IsMobileDevice(HttpRequest r){
String userAgetnt = r.UserAgent;
String deviceName = "Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini";
return Regex.IsMatch (r.UserAgent, deviceName);
}
To detect if request came from mobile app, you must pass Accept:application/json in each request, then in your controller detect if request expects or wants json, if request expects json then return json response otherwise do what you want.
I wrote a simple web server in C that listens for a connection and send some text over a socket.
I want my server to be accessed by a server. In this case I should s*end headers such as CODE(200), Content type, Content length*.
How these headers can be sent to the client(browser or telnet)?
How headers of request can be extracted?
(Maybe, I just don't understand the question, vote me down.)
The http response header format is not too difficult:
header1\n
header2\n
<empty line>\n
content\n
content\n
That's all. The webserver should send the headers, an empty line, then the content.
If you wanna test header sending, you should check it with a browser. Add a line to your webserver to send the following header:
Content-Disposition: attachment; filename=download_me.txt
So when you connect to your webserver with a browser, it will pop up a "save as" dialog instead of displaying the downloaded web page in the browser window. If you got the dialog, and the downloaded file is also OK, your webserver sends the headers properly.
G
It's another issue, wether the client handles them as you (and W3C) expect...
Edit: HTTP ok status is:
HTTP/1.1 200 OK\n
Google for more status codes.