We are calling an API which is external to us and we have no control over. When the data is returned characters are encoded with �. We are using fetch in javascript like so:
return fetch(apiUrl,
{
method: "GET",
headers: {
'Accept': 'application/json, charset=UTF-8',
"Content-Type": "application/json; charset=UTF-8;",
'Accept-Charset' : 'utf-8'
}
})
.then((response) => response);
The first thing we do with the response is call response.json(). At this point the � has replaced any currency symbols or accented letters. I have tried immediately calling response.text() to get the raw text with the same result. Also when I view the result in Fiddler I am seeing � instead of currency symbols.
I have added UTF-8 in the headers to try and accept the right data but with no luck, is this an unsolvable issue down to the external API (internal to company but cannot be changed or exposed here) or am I missing something obvious?
UPDATE:
Following the suggestion below I have removed charset and re-run and interestingly if I view the response in fiddler on the HexView tab it is displaying correctly (although it says "Response body is encoded. Click to decode"), once I click that it displays correctly in HexView. It isn't in the TextView, Raw or JSON tab. The beginning of the HexView data shows:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/plain
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Date: Thu, 26 Jul 2018 07:52:40 GMT
Content-Length: 14876
Related
Does anyone know if there is a way to create an instance of NSHTTPURLResponse from a text file containing a raw HTTP response?
As an example, if I type curl -D - http://google.com in to terminal, I get the following:
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 25 Mar 2022 18:30:04 GMT
Expires: Sun, 24 Apr 2022 18:30:04 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
here.
</BODY></HTML>
Note how the raw response includes:
The HTTP protocol
The response code and any message about the code
All of the response headers
And then, the actual body of the response.
I'd like to be able to somehow init an HTTPURLResponse from the raw HTTP data so it gets all the properties provided: response code, headers, etc, as well as the body.
The purpose of this would be for tests.
We currently exercise our response handling code using files holding data for just the response body. It'd be really convenient to also include other response details so that these can easily be exercised as well.
Ideally this would use the same code that iOS / Mac will be running to completely capture those element of the stack as well.
I am trying to fetch a CSV file from a website (https://www.stocknet.fr/accueil.asp) using a GET request on the https URL. The response I get via Postman looks like this:
Type;Groupe Acc�s;Code;EOTP autoris�s;Familles EOTP autoris�es;Nom;Pr�nom;Adresse Mail;Agences autoris�es;D�p�ts autoris�s;Date cr�ation;Fournisseurs autoris�s;Classes autoris�es;Familles article
But when access the URL directly, my browser automatically downloads the file, and I open it on windows with a proper encoding:
Type;Groupe Accès;Code;EOTP autorisés;Familles EOTP autorisées;Nom;Prénom;Adresse Mail;Agences autorisées;Dépôts autorisés;Date création;Fournisseurs autorisés;Classes autorisées;Familles article
When I inspect the website HTML, I can see the tag <meta charset="ISO-8859-1" />
I tried using headers as such:
Accept-Charset: ISO-8859-1
Accept-Charset: UTF-8
Content-Type: text/csv; charset=ISO-8859-1
Content-Type: text/csv; charset=UTF-8
Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: identity
Content-Encoding: br
Nothing seem to return a response with the correct encoding.
Any idea what I am doing wrong ? Note that, whatever page of the website I try to fetch, I get this wrong encoding. It's not only with the CSV file.
The server is returning content in iso-8859-1 and telling you it's iso-8859-1. You will not convince the server to return anything else. Your web browser contains code to convert encodings. If you want to have the content in a different encoding, you have to convert it yourself.
For ways how to do that, see:
Best way to convert text files between character sets?
How to indicate that one particular header of a request is optional with the markdown of apiary?
My code:
Request
The apiKey can be passed in the header (note the camelcase for "apiKey")
The Content-Encoding: gzip is optional, only if you gzip the entity
Headers
apiKey: `42452-eofefo-4534535` (optional)
Content-Type: application/json
Content-Encoding: gzip (optional)
Is the above ok?
As of January 2016, it's not possible.
On the other hand—if you go to the API Blueprint Roadmap, there's MSON Parameters and Headers feature which is currently in the RFC stage; this will enable you to describe HTTP Headers in the MSON syntax and mark each HTTP Header as required/optional (see below).
+ Response 200 (application/json)
+ Headers
+ Date (Date, optional) - Date at which the message was originated
Hoping MSON Headers will land soon.
I'm trying to set up a small Silex RESTFul server, and now I'm playing around with HTTP Headers.
One GET function checks If-Modified-Since header, and compares with database's Last-Modified item.
Here's a small code sample:
if($hasModifications){
$response = [items]
$statusCode = 200;
} else {
$response = ['result'=>'no modifications'];
$statusCode = 304;
}
return $app->json($response,$statusCode,$headers);
Whenever $statuscode!=200, I only get a blank page and also no custom header gets sent. This also breaks CORS because I can't set Access-Control-Allow-Origin header.
Should I be doing it some other way?
(PS: this is not the actual code, and also if I replace $statusCode for 200, then everything works back as normal)
Update:
So far, I guess from what I've been reading, that 304 statuses shouldn't send anything on response's body. But what about headers? If I don't comply with CORS, then the AngularJS Frontend will fail and I wouldn't be able to catch the "Not Modified" exception!.
Obviously, I can figure it out some other way. But it would be nice if I could have both things working properly (REST+CORS)!
Update 2:
var_dump($headers)
array(4) {
["Last-Modified"]=> string(29) "Fri, 21 Aug 2015 22:47:53 GMT"
["X-Status-Code"]=> int(304)
["Cache-Control"]=> string(25) "post-check=0, pre-check=0"
["Pragma"]=> string(8) "no-cache"
}
Notice that I'm using X-Status-Code to inform the true HTTP Status.
Right now, the response line of code goes as follows:
return $app->json($response,200,$headers);
If I changed 200 to 304, I can't see the output of var_dump (gives me an empty page). Also if I set 304 status with no header parameter, it is still the same.
Seems like return or $app->json clears the HeaderBag, since I'm using JDesrosiers\Silex\Provider\CorsServiceProvider(), and those headers parameters also get cleared.
This is the Raw HTTP Response taken from Firefox with Status Code 304:
Cache-Control: post-check=0, pre-check=0, private
Connection: Keep-Alive
Date: Sat, 22 Aug 2015 19:52:18 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.7 (Ubuntu)
And this, with Status Code 200:
Cache-Control: post-check=0, pre-check=0, private
Connection: Keep-Alive
Content-Length: 25
Content-Type: application/json
Date: Sat, 22 Aug 2015 19:53:12 GMT
Keep-Alive: timeout=5, max=100
Last-Modified: Fri, 21 Aug 2015 22:47:53 GMT
Pragma: no-cache
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.11
X-Status-Code: 304
Apache strips out CORS headers on 304 status codes.
You can read more in issue #51223 and you can also find a patch which will fix this problem.
If you don't want or you can't recompile Apache, you could try running Nginx, which will leave the CORS headers in place.
I have integrated Facebook login into my web application.
I am getting the "code" parameter when i am requesting by sending the required details, and then i have to exchange the obtained "code" for an "access_token".For that i am making a HTTP GET call to :-
https://graph.facebook.com/oauth/access_token?
client_id={app-id}
&redirect_uri={redirect-uri}
&client_secret={app-secret}
&code={code-parameter}
The response which i am getting is
HTTP/1.1 200 OK [Content-Type: text/plain; charset=UTF-8, Access-Control-Allow-Origin: *, X-FB-Rev: 1624746, Pragma: no-cache, Cache-Control: private, no-cache, no-store, must-revalidate, Facebook-API-Version: v1.0, Expires: Sat, 01 Jan 2000 00:00:00 GMT, X-FB-Debug: XM3kCi/2T3bPfe2/QRmn08rBwHTn7SkY8iADhkirVCZ4HOKPGH45zK8WWq0/5+KDFdUWwIn7blnjYfy1py6jrQ==, Date: Wed, 04 Mar 2015 06:25:17 GMT, Connection: keep-alive, Content-Length: 245]
In the response i am not getting the access_token parameter which i need for further proceeding into the application.
According to Facebook docs response should look like
access_token={access-token}&expires={seconds-til-expiration}
Any help on this is highly appreciated.
Finally managed to get the solution for the answer.Facebook gives the access_token but it needs to be retrieved using entityutils object and then converted into UTF-8 type.here is the code which worked for me and i am able to get the access_token object from the Facebook API response.
HttpGet request = new HttpGet(FBAPI_HITURL.toString());
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
responseString will contain the response in the format "access_token={access-token}&expires={seconds-til-expiration}"