Python requests post reponse 400 - python

I have no idea where is error. My response is 400 cuz of it:
"errors":{"data":["The data field is required."
but i am sending 'data':
data = "{'example': 'example'}"
url = f'http://localhost:5219/myApi'
payload = {'data': str(data).encode('utf-8')}
req = requests.post(url, data=payload)
print(req.content)
response
b'{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-355ed090aff5fa10460587fe222edbbe-3b3927cf18b26b78-00","errors":{"data":["The data field is required."]}}'
how can i repair that?
i tried changing formats and other things but it didn't work pls help me
this is c# code from api:
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using System.Text.Json.Nodes;
namespace spyware.Api.Controllers
{
[Route("[controller]")]
[ApiController]
public class api : ControllerBase
{
[HttpPost]
public void GetResponse(string data)
{
Console.WriteLine(JsonConvert.DeserializeObject(data.ToString()));
}
}
}

Related

Put/Post errors while trying to access it from through python. "Bad Request. The JSON payload should be inside a root property called

I am trying to post/put to Sheety via Python:
SHEETY_URL = "https://api.sheety.co/gfhgfhjfdghjgfjf/flightDeals/prices/5"
header = {
"Content-Type" : "application/json"
}
params_sheety = {
"price": {
"iataCode": "PLN"
}
}
response_sheety = requests.put(url=SHEETY_URL, params=params_sheety, headers=header)
print(response_sheety)
print(response_sheety.json())
=======================================================
Getting Bad request Error:
<Response [400]> {'errors': [{'detail': "Bad Request. The JSON payload
should be inside a root property called 'price'. Check
https://sheety.co/docs for more details."}]}
The same request works fine with Postman.
When you use params=params_sheety, the parameters are not sent as json.
Use json=params_sheety to send them as json.
This also sets the content-type header to json automatically, so you don't need headers=header.

login into Spotify through python requests module

I've been trying to connect my Spotify account using the requests module and I came across a problem
later on I tried to manipulate it on my Python code
import requests
URL = 'https://accounts.spotify.com/login/password'
payload = {
"username": "example#gmail.com",
"password": "123456",
"remember" : True
}
response = requests.post(URL , json=payload)
print(response.status_code)
OUTPUT : 415
what went wrong ?
I don't think this is how you should interact with Spotify from code.
It has an API and you should use tokens for authentication or anything else from password.
Anyway you can try setting the correct media type when making the request:
URL = 'https://accounts.spotify.com/login/password'
payload = {
"username": "example#gmail.com",
"password": "123456",
"remember" : True
}
headers = { 'Content-Type':'application/x-www-form-urlencoded' }
response = requests.post(URL , json=payload, headers=headers)
you cannot use this url to send post request as it has "recaptchaToken" which you need pass in your payload which is getting generated dynamically. Hence it is not a good idea to use this approach.
Instead you can use API to achieve the same.
https://github.com/plamere/spotipy

POST request payload in python not reaching PHP server

I am trying to send a JSON payload with session request in python to a PHP server.
The session itself is established and cookies are accepted by the server but when I print out the $_POST, it returns an empty array.
It looks like the payload is not being sent with the request or maybe PHP doesn't recognize it.
Client Side - Python:
import json
import requests
url = 'https://prov.is.st.com/eventpost.php'
payload = {
'script': 'pyt.sh',
'status': 'Success'
}
s = requests.Session()
s.cookies.set('provisioning', cookie, domain='prov.is.st.com')
headers = {
'Content-Type': 'application/json'
}
s.headers.update(headers)
response = s.post(url, data=payload, verify=False)
print(response.text)
Server Side - PHP:
<?php
print_r($_POST);
if(isset($_POST['status'])){
$status = $_POST['status'];
}
else{
$status=0;
}
if (isset($_POST['script'])) {
$script=$_POST['script'];
} else {
$script="unknown";
}
if (isset($_COOKIE['provisioning'])) {
$cookie=$_COOKIE['provisioning'];
print "OK: Message accepted: [$status][$script]\n";
}
else {
print "ERROR: NO cookie provided.\n";
}
Output
Array
(
)
OK: Message accepted: [0][unknown]
As per the documentation here: https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests, in order to post JSON data, you should use either of the following ways:
import json
...
response = s.post(url, data = json.dumps(payload), verify = False)
or
...
response = s.post(url, json = payload, verify = False)
Note, the json parameter is ignored if either data or files is passed.
Using the json parameter in the request will change the Content-Type in the header to application/json.

requests post from django to nodejs

I trying to send data from django to nodejs. I receive but can't read data params.
Django:
response = {
"status": "success",
"voto": all_votes,
"user_choice": new_vote.value if new_vote else None
}
import requests
requests.post('http://localhost:8081/vote_added', data = response)
NodeJS:
//router/index
router.post('/vote_added', votes.vote_added);
//votes controller
module.exports.vote_added = function(req, res) {
console.log("test", req.params)
console.log("test2", req.body)
console.log("test3", req.query)
}
ps: my response is a dict
I fix that. Just change data to params in django request:
requests.post('http://localhost:8081/vote_added', data = response)
to
requests.post('http://localhost:8081/vote_added', params = response)
and in nodejs, work with req.query

How to use header key from the api to authenticate URL in iOS Swift?

I am using Alamofire for the HTTP networking in my app. But in my api which is written in python have an header key for getting request, if there is a key then only give response. Now I want to use that header key in my iOS app with Alamofire, I am not getting it how to implement. Below is my code of normal without any key implementation:
Alamofire.request(.GET,"http://name/user_data/\(userName)#someURL.com").responseJSON { response in // 1
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result)
}
I have a key as "appkey" and value as a "test" in my api. If anyone can help. Thank you!
This should work
let headers = [
"appkey": "test"
]
Alamofire.request(.GET, "http://name/user_data/\(userName)#someURL.com", parameters: nil, encoding: .URL, headers: headers).responseJSON {
response in
//handle response
}
let headers: HTTPHeaders = [
"Accept": "application/json",
"appkey": "test"
]
Alamofire.request("http://name/user_data/\(userName)#someURL.com", headers: headers).responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result)
}

Categories

Resources