I have the following code to post using requests module
api_path = r'/DeviceCategory/create'
api_server = (self.base_url + api_path)
logging.info("Triggered API : %s", api_server)
arguments = {"name": "WrongTurn", "vendor": "Cupola", "protocolType": "LWM2M"}
headers = {'content-type': 'application/json;charset=utf-8', 'Accept': '*'}
test_response = requests.post(api_server,headers=headers,cookies=self.jessionid,
timeout=30, json=arguments)
logging.info(test_response.headers)
logging.info(test_response.request)
logging.info(test_response.json())
logging.info(test_response.url)
logging.info(test_response.reason)
The following response i got in header
2017-08-22 12:03:12,811 - INFO - {'Server': 'Apache-Coyote/1.1', 'X-FRAME-OPTIONS': 'SAMEORIGIN, SAMEORIGIN', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, DELETE, PUT', 'Access-Control-Allow-Headers': 'Content-Type', 'Content-Type': 'text/html;charset=utf-8', 'Content-Language': 'en', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Date': 'Tue, 22 Aug 2017 06:33:12 GMT', 'Connection': 'close'}
And JSON decoding the error
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Can some please help me out, the status code i got is 500
It means that the server didn't return any response values (there was no response body, so json() couldn't decode the JSON). Given the 500 error, that probably means that your API call was bad. Not being familiar with the API, I cannot say more but my guess is that you are passing the arguments wrong, try something like:
test_response = requests.post(api_server,headers=headers,cookies=self.jessionid,
timeout=30, data=arguments)
Related
please help to resolve the issue, I sent payload, but get error 400:
Post:
payload = {"country_code":"XX","phone_number":"1234567890"}
r = requests.post("https://site.su/jwt-auth/start", params=payload, headers=headers, verify=False)
r.json()
{'statusCode': 400,
'reason': 'invalid_request',
'errorId': 'c2114dc8-0632-49c6-8cfb-cc5b08897ecc',
'message': 'Validation failed: \n -- PhoneNumber: phone_number is reuiqred Severity: Error',
'localizedMessage': 'One of parameter was sent with an error. Please try again or go back to the previous page.',
'invalidParameters': {'phone_number': {'reason': 'NotEmptyValidator',
'localizedMessage': 'phone_number is required'}}}
Content-Disposition: form-data; name="cd[formFeatures]"
[{"id":"","name":"country_code","tag":"input","inputType":"text"},{"id":"","name":"","tag":"input","inputType":"text","valueMeaning":"empty"},{"id":"","name":"phone_number","tag":"input","placeholder":"+7 (___) ___-__-__","inputType":"tel"}]
Payload
I have provided my code.
I am a network person trying to use python. I am trying to use rest api on one of the devices and use it through python 3 using the requests library.
url = 'https://erspan/api/?type=op&cmd=<show><system>'...not a complet url
rs = requests.get(url, verify=False)
print(rs.headers)
print(rs.text)
{'Date': 'Thu, 09 May 2019 21:34:39 GMT', 'Content-Type': 'application/xml; charset=UTF-8', 'Content-Length': '1891', 'Connection': 'keep-alive', 'ETag':.....}
<response status="success"><result><system><hostname>erspan</hostname><ip-a.......
the object type for this is
print(type(rs))
<class 'requests.models.Response'>
I want to get this as json
I am building an app using ionic/cordova that will take a scan, store this to the device (this part works), and also upload this data to a server (this part doesn't work).
The server is a RESTful api that expects JSON to be POSTed (I didn't create the server, these are the details I have been given).
When I try to post to the server I am getting the following error -
{status: 415, url: "http://jsfitnessservice.azurewebsites.net/api/values123", headers: {…}, error: ""} error:""
headers:
content-length:"0"
date:"Tue, 06 Feb 2018 19:41:36 GMT"
request-context:"appId=cid-v1:bf5436d9-7e1e-491e-b79d-d40d9b401307"
server:"Kestrel"
x-android-received-millis:"1517946100312"
x-android-response-source:"NETWORK 415"
x-android-selected-protocol:"http/1.1"
x-android-sent-millis:"1517946100070"
x-powered-by:"ASP.NET"
My code -
let httpData = {
"id": this.device.uuid,
"scanDateTimes": this.lastScans,
"appVersion": "Version 1.0"
}
let header = new Headers();
header.append('Content-Type', 'application/json; charset=utf-8');
this.http.post('http://jsfitnessservice.azurewebsites.net/api/values 123', httpData, header).then
(data =>{
console.log("Data Status - ", data.status);
}).catch(error =>{
console.log("Error - ", error)
});
I have tried a few things that I have seen online, such as adding the header to an object, or using JSON.Stringify() around the data I am sending. But these give errors too.
JSON.stringify around data gives this error -
status: -1, error: "unsupported params type, needs to be a JSON object"
Making header an object gives this error -
status: 0, error: "advanced-http: header values must be strings", headers: {…}}
Have you tried using
this.http.setDataSerializer(‘json’)
this.http.post('http://jsfitnessservice.azurewebsites.net/api/values 123', httpData, {'Content-Type': 'application/json; charset=utf-8'}).then
(data =>{
console.log("Data Status - ", data.status);
}).catch(error =>{
console.log("Error - ", error)
});
I am constrained to using AFNetworking, but I am working in Swift. I am trying to retrieve data from a POST API request that is supposed to be returning JSON. However it seems that no matter how I make the request I am not able to see what is being returned and that it does not seem to be in proper JSON format. Here's what I've tried.
(1) The canonical approach:
manager.responseSerializer = AFJSONResponseSerializer.init(readingOptions: .AllowFragments)
manager.responseSerializer.acceptableContentTypes = serialiazationTypes
This produces the error: Invalid value around character 2
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 2." UserInfo={NSDebugDescription=Invalid value around character 2., NSUnderlyingError=0x60800004ad70 {Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x608000a2c140> { URL: https://API_ADDRESS} { status code: 400, headers {
Connection = "keep-alive";
"Content-Length" = 2261;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 07 Dec 2016 15:46:26 GMT";
Server = "nginx/1.10.1";
} },
(2) Using the standard parser. So basically I removed any explicit setting of the response serializer, which produced this error:
Optional(Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSUnderlyingError=0x60000004ace0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x61800082e6a0> { URL: https://API_ADDRESS } { status code: 400, headers {
Connection = "keep-alive";
"Content-Length" = 2261;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 07 Dec 2016 15:49:12 GMT";
Server = "nginx/1.10.1";
(3) So then I tried to read as text/html, not requiring JSON, so that I could see what the problem is, but I don't seem to be able to retrieve this and just see what the content is either. So with this code:
manager.responseSerializer = AFHTTPResponseSerializer()
I ended up with this response, which does not even indicate why this is a bad request:
"Request failed: bad request (400)"
{ status code: 400, headers {
Connection = "keep-alive";
"Content-Length" = 2261;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 07 Dec 2016 15:51:10 GMT";
Server = "nginx/1.10.1";
} }
Here are my questions
(1) In cases 1 & 2 above are these problems with parsing the response code whereas in case 3 I am just seeing the server tell me that my request is bad?
(2) How can I start figuring out why my request is bad? This is an internal API, but I'm not getting a lot of help with how it works. I have a sample curl command that does work with the API, but I'm not clear on how I am departing from it:
curl -X POST -v -s -H "Session-Token: EYYW$YW$YWYW" -H "Content-type: application/json" -d#sample.json https://API_ADDRESS
What is different between that and what I am doing below:
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFHTTPResponseSerializer()
manager.requestSerializer.setValue(sessionToken, forHTTPHeaderField: tokenString)
manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-type")
manager.POST(
user_url,
parameters: nil,
success: { (operation: AFHTTPRequestOperation?,
responseObject: AnyObject!) in
print("JSON FROM POST: \(responseObject)")
},
failure: { (operation: AFHTTPRequestOperation?, error: NSError?) in
print("there was an error in POST: \(error)")
}
)
I´m calling:
events: {
url: '/CondominioVip/evento/evento_json.json',
error: function() {
alert('there was an error while fetching events!');
}
}
I've also tried to add type: 'POST but it didn't work either.
My controller:
def evento_json():
events= "[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00','allDay':False}]"
return events
Test call from browser (http://localhost:8000/CondominioVip/evento/evento_json.json):
[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00','allDay':False}]
Request from web2py ajax function:
Accept:application/json, text/javascript, /; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:31
Content-Type:application/x-www-form-urlencoded
Cookie:session_id_admin=127.0.0.1-f9db7d99-4e7e-4bae-a229-d6614e9599f0; cvip_language=pt-BR; session_id_condominiovip=127.0.0.1-b820cbe3-9a55-40bb-8618-8d3f9a43f7c2
Host:localhost:8000
Origin:http://localhost:8000
Referer:http://localhost:8000/CondominioVip/evento/index/2
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5
X-Requested-With:XMLHttpRequest
Response headers:
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Length:105
Content-Type:application/json
Date:Tue, 29 May 2012 02:54:04 GMT
Expires:Tue, 29 May 2012 02:54:03 GMT
Pragma:no-cache
Server:Rocket 1.2.4 Python/2.7.3
Set-Cookie:cvip_language=pt-BR; expires=Wed, 30-May-2012 02:54:04 GMT; Path=/, session_id_condominiovip=127.0.0.1-b820cbe3-9a55-40bb-8618-8d3f9a43f7c2; Path=/
X-Powered-By:web2py
Response:
[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00','allDay':False}]
I'm out of options. Any help would be apprecianted.
Update:
I've installed the JsonView extension on Chrome, so returning a string is not considered a json response.
I made some changes:
def evento_json():
rows = db(evento.id>0).select(evento.id,evento.titulo,evento.data_hora_inicio,evento.data_hora_fim)
events = []
for row in rows:
event = {'title': row['titulo'],
'start': row['data_hora_inicio'],
'end': row['data_hora_fim'],
'allDay': False,
'url': URL(c='evento', f='index', args=[row['id']], extension=False)}
events.append(event)
return events
But web2py throws an error. I´ve printed "events" and "json(events)" before send it to generic.json, and the format is exactly what fullcalendar expects.
The way I found to stop the error is:
In controller:
def evento_json():
import datetime
start = datetime.datetime.fromtimestamp(int(request.vars['start'])).strftime('%Y-%m-%d %H:%M:%S')
end = datetime.datetime.fromtimestamp(int(request.vars['end'])).strftime('%Y-%m-%d %H:%M:%S')
set = db((evento.id>0) &
(evento.data_hora_inicio >= start) &
(evento.data_hora_fim <= end))
if (not auth.has_membership(auth.id_group(role='site_admin'), auth.user.id)) and \
(not auth.has_membership(auth.id_group(role='cond_admin'), auth.user.id)):
set = set(evento.flag_disp==True)
rows = set.select(evento.id,
evento.titulo,
evento.data_hora_inicio,
evento.data_hora_fim,
evento.flag_disp)
events = []
for row in rows:
event = {'title': row['titulo'],
'start': row['data_hora_inicio'],
'end': row['data_hora_fim'],
'allDay': False,
'url': URL(c='evento', f='index', args=[row['id']], extension=False),
'color': 'blue' if row['flag_disp'] is True else 'red'}
events.append(event)
if events:
from gluon.serializers import json
return XML(json(events))
else:
return '{}'
And create a view evento/evento_json.json with the following:
{{=response._vars)}}
It works! But seems a bug to me. I'm not sure if I'm doing something wrong.
Maybe try:
events= "[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09T12:30:00Z','allDay':false}]"
(Changed the event3 start date/time to ISO8601 format as per the API, and changed "False" to "false".)