Network request failed in React native using Fetch API - react-native

I'm using fetch API to get data from server using POST method. It return an error: Network request failure. when I try with a Facebook movie API it works good, then I try it with Postman width same key and userId and everything is okay
this is what I have when I click on the TestAPI button
this is result in Postman width same configurations

The response that you are showing does not seems to be a JSON object (see the encoding issues) so when you try to do response.json() most likely fails because it can't parse it.
Try to log the response or do response.text() instead of response.json() to try it out.
Seems like your problem is on your php server code.

Related

Why Postman send empty data in POST method

When I develop POST api, I used POSTMAN I create following request
But it seems send empty request to servers.
When I check server, the following error was displayed
Error: data and salt arguments required
It means empty body was send via POSTMAN
On the other hand ,When I tried
curl --data "loginId=hikaru&password=test" http://localhost:3000/user
following error was occured but some data was send and reached
ER_NO_DEFAULT_FOR_FIELD: Field 'firstName' doesn't have a default value
So what is the difference between POSTMAN and command ?
What is the wrong point of postman
I'd like to fix it.
If someone has opinion,please let me know.
Thanks

Postwoman API Tester response is always empty

Postwoman (now Hoppscotch) is an API request builder (like Postman) that I love the idea of because it's open source, but I'm not getting any response showing up in the Response field. It's a private API I'm hitting so I'd rather not show my request but it's a POST and my API is sending back the correct response (that works fine in Postman). I'm seeing the following error in the browser console:
TypeError: text.match is not a function
Is anybody else having this problem?
Adding browser extension:
https://addons.mozilla.org/en-US/firefox/addon/hoppscotch/
done the trick for me

How to distinguish between GET and POST

I'm writing a simple api for training using express. Here's my testing code:
var express = require('express');
var app = express();
app.post("/api/:var_name", function(req, res) {
res.send(req.params.var_name);
});
is simply testing to see if POST is working. When I call http://localhost:3000/api/1 I get Cannot GET /api/1, so the server is obviously interpreting the POST request as GET, what do I need to do to call POST instead?
Anything you call in the address bar of your browser will be sent via get. This is due to the fact that post-messages (and almost all other methods) do have a body-part. But there is no way for your browser to send additional information inside the body of the http packet.
If you want to test your routes for any method other than GET I would suggest you download a tool like postman.
https://www.getpostman.com/
BEWARE: This is my preference. You can of curse also use text based browsers like curl to test it.
The server interprets the request according to the verb you set in the HTTP request. If no method/verb is specified it is interpreted as GET(not sure about this part).
When you call that URL, you need to use the method as well. For example if you use the fetch API, you can call it like:
fetch(url, {method:"POST"})
If you're entering it in your browser and expect it to be interpreted as a post request, it's not. All browser url requests are GET. Use a tool like Postman to call different HTTP verbs. It's really useful when creating such APIs.
You can check out this answer on details of how to add body and headers to a post request: Fetch: POST json data

How to send a POST request response back to client (React component)?

I'm using React to send a post request to my express server which then returns a response, like so:
app.post('/api/games', function(req, res) {
return res.json();
});
Before I use that response to persist it to a database, I want to be able to see everything within that response. When I console.log it from the server, I get a long list of the JSON object printed, but would ideally like something closer to Chrome's dev tools console where I can click through and inspect everything. How can I send this response JSON back to my React component so that I can console.log it in in the browser?
Thanks!
You can use node-inspector in order to debug your express API.
It uses Chrome Dev Tools so you'll feel like you are debugging your client side!
Remember, a request comes in, and the response goes out.
on a request, the req.body and req.params will contain the body/params of the request.
To respond to a request, use the res.json({someJSONHere}) or you can also res.send({someJson})
You can also console.log inside the server and watch output on the terminal
app.post('/api/games', function(req, res) {
console.log(req.params)
console.log(req.body)
return res.json(
Object.assign({}, req.body, req.params)
);
});
Express request/response Documentation
If you want to view your response in the client-tier here are some of the things you can do using the GoogleChrome asides from the console
1 Use React Developer Tools
2 I like using Postman for viewing and manipulating request/response

How to use offset on Telegram bot API webHook

Since 2 days I've been exploring the Telegram bot API, which is pretty neat. But there is one thing I can't figure out.
When you don't use the webHook but the /getUpdates call, you can tell the API via the offset parameter which message where processed by the server.
But how do you do this with the webHook in place? I keep getting the same message as an update. Which results in the server spamming the user with the same message.
The solution I came up with is as follows:
Receive an update from the webhook
Save the update_id
Reply to the user /sendMessage
disable the webHook /setWebhook?url=
Set the offset /getUpdates?offset={update_id+1}
Reinstate the webHook /setWebhook?url=https://mywebhook.domain.com
There must be a better way right? Anyone?
Ok, problem solved. It appeared that just a 200 (OK) wasn't enough (the body of my response was null. I've added a body to the response {}, and know it works fine.
You must say to telegram that you get updates successfully with this:
- 200 response code
&
- empty json like this {}
use This on webHook to get data from telegram servers:
// get the raw POST data
$rawData = file_get_contents("php://input");
// this returns null if not valid json
$jsonData = json_decode($rawData);
What HTTP status code are you returning on the page handling your webhook? It is possible that Telegram is attempting to retry your webhook endpoint because it's not receiving a status 200 (OK) from you.