POST Request in Jupyter notebook - api

i want to get id token from my credentials, i have an url, header, and body from client:
i do code in jupyter like this but did not work, any idea?
import requests
import json
import os
url = "https://xxxxx"
querystring = {"page":"0","limit":"50","sort":"desc"}
payload = "{'key':'value'}"
headers = {
'Content-Type': "application/json"
}
body = {
'email': "{{email}}",
'password': "{{password}}"
}
resp = requests.request("POST", url, data=payload, headers=headers, body=body)
respText = json.loads(resp.text)
respText['data']

Related

Getting error while framing request URL using appscript

I am trying to frame request for API using appscript.
var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe"
let body = {
'"url"':'"https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg"'
};
const headers = {
'method' : 'POST',
'Host':'imagealttextcreation.cognitiveservices.azure.com',
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key':'###',
'payload': body
};
var response = UrlFetchApp.fetch(url_string,headers)
Logger.log(response)
I am getting invalid request error. But the same thing is working when i try manually(attached image for same).
Am I missing something while forming this request in the appscript?
When tried manually using the browser the functionality works. i want help in correcting the request using appscript.
From the official document, in your script, how about the following modification?
Modified script:
var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe";
let body = { url: "https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg" };
const options = {
headers: { "Ocp-Apim-Subscription-Key": "###" }, // Please set your value.
payload: JSON.stringify(body),
contentType: "application/json"
};
var response = UrlFetchApp.fetch(url_string, options);
Logger.log(response.getContentText())
Reference:
fetch(url, params)

upload file with aiohttp same as requests library

i want upload a file from local server to host with api and work with requests library but i don't know how do this with aiohttp
code with requests library
url = "https://example.com/upload"
payload={'Domain': 'yourdomain','path': '/'}
files=[
('file',('test.txt',open('your file path','rb'),'text/plain'))
]
headers = {
'Authorization': 'yourtoken'
}
response = requests.request("POST", url,
headers=headers, data=payload, files=files)
print(response.text)
aiohttp code
url = "https://example.com/files/upload"
headers = {
'Authorization': 'yourtoken'
}
fields = {'Domain': 'yourdomain',
'path': '/',
'file':('test.txt',open('your file path','rb'),'text/plain')
}
async with aiohttp.ClientSession() as session :
async with session.post(url,data=fields,headers=headers) as res :
print(res.text)
just return 200 [ok]

React native file upload using dropzone to flask

Hi I want to upload file using React native to flask server
I sent a file using fetch function and I received response. But print(request.files) result ImmutableMultiDict([]) how to I see my file in server? I checked state was right and I got response {'state' : 'ff'}
react native
pressSumbitButtom() {
var formData = new FormData()
formData.append('file', this.state.file)
return fetch('http://127.0.0.1:8000/file/', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formData
}).then(res =>
res.json()
).then(data => console.log(data))
}
App.py
#app.route("/file/", methods=['GET', 'POST'])
#cross_origin(origin='*', headers=['Contect-Type'])
def get_filepath() :
response = jsonify({'state':'ee'})
if request.method == 'POST' :
print('response: ',response, 'request: ', request)
print(request.files)
response= jsonify({'state':'ff'})
return response
return response

Constructing the UrlFetchApp request to obtain an Access Token from ChinaBrands API

function getAccessToken()
{
var url = "https://gloapi.chinabrands.com/v2/user/login";
var data = {
"email": EMAILID,
"password": PWD,
"client_id": APIKEY
}
var payload = {
"data": data,
"signature": Utilities.base64Encode(data + SECRETKEY)
}
var options = {
"method": "post",
"contentType": "application/json",
"payload": payload
};
var json = UrlFetchApp.fetch(url, options).getContentText();
Browser.msgBox(json);
}
Please see attached documentation API screenshot link below.
I am trying to connect ChinaBrands API using google apps script.
I am getting error which says "{"status":0,"msg":"Requested info is empty","errcode":10006}"
See this Image : Documentation API to get AccessToken:
The documentation is a bit misleading. You need to make POST request with Content-Type: application/x-www-form-urlencoded. The request data field should be a string with your data in JSON format. I guess depending on language/libraries you use, you might need to URL encode the JSON data before making the request.
The resulting request should look like:
POST https://gloapi.chinabrands.com/v2/user/login
...
Accept: */*
Content-Type: application/x-www-form-urlencoded
signature=<md5 of string containing data in json + client secret>&data=%7B%22email%22%3A+%22name%40example.com%22%2C+%22password%22%3A+%2222password1%22%2C+%22client_id%22%3A+%1337%22%7D
Python 3 example with requests library:
url = "https://gloapi.chinabrands.com/v2/user/login"
data = {...}
json_data = json.dumps(data)
signature = md5((json_data + client_secret).encode('utf')).hexdigest()
response = requests.post(url, data={'signature': signature, 'data': json_data})

TypeError: Network Request Failed at XMLHttpRequest in react native app

I'm using react native fetch to make a post http request with graphql data to a graphql server endpoint. When starting up my graphql server and calling fetch with a POST in my react native app, I'm getting the following error:
TypeError: Network Request Failed at XMLHttpRequest in react native app
Request code
let graphqlServer = config.graphQLServerUrl;
const graphQLFetcher = (graphQLParams, requestType, token) => {
let body = null; //earlier i used var body =' '; //blank value --null & blank makes a different
let headers = null;
body = graphQLParams;
headers = {
'Accept': 'application/json',
'Content-Type': 'application/graphql',
'Authorization': 'Bearer ' + token
};
return fetch(graphqlServer, {
method: requestType,
headers: headers,
body: graphQLParams,
});
};
Data
qraphQLParams
query {
login(userName: ${credentials.userName}, passWord: ${credentials.passWord})
}
,
Any ideas?
I managed to get this to work by simply JSON.stringify the body of the request.
So..
graphQLParams = `query login{
login(input: {userName: "${obj.input.userName}", passWord: "${obj.input.passWord}" }) {
token
}
}
`
body = JSON.stringify({
query: graphQLParams
});