capacitor-community / http

Community plugin for native HTTP
MIT License
208 stars 136 forks source link

I'm unable to get a rest service's response, because it's a string #177

Open ciccilleju opened 3 years ago

ciccilleju commented 3 years ago

hello, i'm doing a post request to a rest service. If i do it on Postman, everything works fine, if I try to call the service with this code i get this error: ` doPostApplicationJson = (url: string, params: any) => {

const options: HttpOptions = {
  url: url,
  headers: {
    'content-type': "application/json",
    'accept': 'application/json',
  },
  data: params,
  method: 'POST'
};

return from(Http.post(options));

};

this.httpService.doPostApplicationJson(this.restService.manualLogin, postData).subscribe(res => { console.log(res); });

`

image because the service returns a string.

How can i solve it? there is a way to accept the string as response?

thank you

ed-turner commented 3 years ago

I am experiencing a similar issue.

These are my HTTP options:

{ url: "https://example.com/api, headers: { 'content-type': "application/json", 'accept': 'application/json', 'mode': 'no-cors' }, data: { 'instances': ['hi'] } }

This is the error I have been getting:

Access to fetch at 'https://example.com/api' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is there something missing here?

I am using:

"dependencies": { "@capacitor-community/http": "^1.3.0", "@capacitor/app": "^1.0.3", "@capacitor/cli": "^3.2.4", "@capacitor/core": "^3.2.4", "@capacitor/ios": "3.2.4", "@ionic/core": "^5.8.2", "@ionic/vue": "^5.8.2", "axios": "^0.22.0", "core-js": "^3.6.5", "vue": "^3.0.0", "vue-router": "^3.5.2" }

npm version 7.24.0

I am testing locally on a Mac iOS Big Sur version 11.6

Thank you

brian-weasner commented 3 years ago

@ciccilleju Have you tried modifying your http "Accept" header to be like this: Accept: application/json, text/plain, */*

@ed-turner Your issue does not seem related to ciccilleju's issue. You are having cors issues. The server in your case does not want to send responses to localhost:8100 because it does not want to share it's resources with you. If this server/service is your own update the cors policy. If the api is not yours but configurable, check with the api's documentation on how to add your domain (in your case localhost:8100 to it's cors list)

ed-turner commented 3 years ago

@ciccilleju Have you tried modifying your http "Accept" header to be like this: Accept: application/json, text/plain, */*

@ed-turner Your issue does not seem related to ciccilleju's issue. You are having cors issues. The server in your case does not want to send responses to localhost:8100 because it does not want to share it's resources with you. If this server/service is your own update the cors policy. If the api is not yours but configurable, check with the api's documentation on how to add your domain (in your case localhost:8100 to it's cors list)

Thank you for the response. You were correct. I found out the issue yesterday (setting the headers wasn’t enough. Had to download an extension to temporarily turn off cors).

ciccilleju commented 3 years ago

@ciccilleju Have you tried modifying your http "Accept" header to be like this: Accept: application/json, text/plain, */*

@ed-turner Your issue does not seem related to ciccilleju's issue. You are having cors issues. The server in your case does not want to send responses to localhost:8100 because it does not want to share it's resources with you. If this server/service is your own update the cors policy. If the api is not yours but configurable, check with the api's documentation on how to add your domain (in your case localhost:8100 to it's cors list)

hello @brian-weasner , thank you for your reply. It actually works in that case. But i'm facing a similar issue with another service, it says: SyntaxError: Unexpected number in JSON at position 4

If i try to make the http request with Postman, it works image

this is my code:

image

image

and if I check my network i actually get the string back, but in the console i get the error and i cant access the response image

sorry for bothering :)

(this is the get request made with postman: ` var myHeaders = new Headers(); myHeaders.append("Cookie", "JSESSIONID=Y1c8cyN7Tf_x72HMnTsZF4dSnm6IBuVvtzQBC9ak.ip-172-31-34-51");

var requestOptions = { method: 'GET', headers: myHeaders, redirect: 'follow' };

fetch("https://MYAPIURL.getParametroString?idParametro=17", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error)); `