Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

How can I construct a POST request body in React Native not with a stringified json but a json?

$
0
0

I working on replace some native-code with react native. The expected POST request (implemented in AFNetworking) in Charles should be like this:

AFNetworking POST

Code snippet:

NSError *err;NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&err];NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&err];   NSString *paramString = [[NSString alloc] initWithData:paramData encoding:NSUTF8StringEncoding];NSDictionary *param = @{@"data":paramString};AFHTTPRequestOperation *operation = [manager POST:URLString parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {    if (successBlock) {        successBlock(responseObject);    }} failure:^(AFHTTPRequestOperation *operation, NSError *error) {    DebugLog(@"%zd", operation.response.statusCode);    if (failureBlock) {        failureBlock(operation, error);    }}];

But the request from Fetch API version is like this:

fetch POST

Code snippet:

export default async (url, param) => {  var result = await fetch(url, {    method: 'POST',    headers: {'Accept': 'application/json','Content-Type': 'text/html',    },    body: JSON.stringify({ 'data': JSON.stringify(param)     })  })  .then(response => checkStatus(response))  .then(response => response.json())  .catch(e => { throw e; });  return result;}

My question is how can I send a post in fetch exactly as in AFNetworking? This cost me a lot of time. Thx!!

updated: The main difference is the annoying slashes and their body data structure, native one is json (data: paramString), while js string.


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>