POST request in Ionic2


#1

I’m sending a post request to the web service but getting the error “forbidden”. Below is my code

getSearchResults1(term: string){
    let data= JSON.stringify({searchString:term});
    let headers=new Headers({'Content-Type':'application/x-www-form-urlencoded'})
    let options = new RequestOptions({headers:headers});
    this.http.post(myUrl,data,options)
    .map(res =>res.json())
    .subscribe(data =>{
      console.log(data);
    });
    
  }

The error I’m getting on console is:

But when I’m manually entering the url with post data appended to it like: myUrl?searchString=Trump, I get the response from web service. I can’t get to the root of the issue.


#2

When you manually write in the data in the URL in your browser it’s a GET request, are you expecting a GET or a POST request in your backend? You should try out Postman to test this kind of stuff really easily :slight_smile:


#3

The app is already made in native IOS. I checked its code and there its a post request


#4

Is there any chance you have IP restriction on your backend ? Forbidden is kinda self explanatory


#5

What I mean though is, what request is the backend expecting? A GET or a POST?


#6

Nope. If that had been the case then we would have been informed


#7

It’s expecting a POST request


#8

Try recreating the exact same POST request in Postman, does it work there? Maybe there is some odd CORS handling API wise


#9

Sure, will try it out


#10

If I throw single post on that URL I get proper response

POST /searcharticle2 HTTP/1.1
Host: regulatorynavsvc-dev.pwc.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3087.0 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache

And response is

{"nodes":[{"node":{"responseCode":"E404","errorMessage":"No records"}}]}

So this seems to be working. Maybe you are just sending something the server does not like.


#11

AFAIK, iOS mandates https URLs.