0

In my current iOS project I need to fetch data from api call with POST method along with login credentials(userName & Password) as authentication header in react native javaScript file.

Can someone Please help me on that.

refernce screen shot

1
  • Update your tag, what framework are you using if you are using JS with iOS ? Commented Dec 28, 2016 at 8:45

1 Answer 1

1

Just set the fetch method to 'POST', add in headers and body as key-value pairs and process response. Here is an example.

var bodyMap = {};
// fill in the body map with keyvalue pair
fetch(POST_URL, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Authorization': authValue,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(bodyMap)
      }).then((response) => response.json())
        .then((responseData) => {
          console.log(responseData);
          //process response 
        })
        .catch((error) => {
          console.warn(error);
        });
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Irfan Ayaz. Can you please explain, how to get the 'authValue' with userName & Password from the snippet ?
That depends upon how the server is expecting it.
Server expecting authorisation value in Base 64.
How in base64? Looks like you are not clear enough. What key-value pair server is expecting username and password in? You can't just generate authvalue from both using base64. I can help you out if you can get the required info from server.
Please guide how can we do with axios in react-native

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.