Trouble with json convert - python-requests

Im just a beginner with python but have a plan to working hard to be expert soon:)
I tried to convert to json nad have error:
Response not in valid JSON format
and
class 'requests.models.Response'>
url="https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna"
try:
response = requests.get(url)
if not response.status_code == 200:
print("HTTP error",response.status_code)
else:
try:
import json
response.content.decode('utf-8')
response = json.dumps(response)
loaded_response = json.loads(response)
except:
print("Response not in valid JSON format")
except:
print("Something went wrong with requests.get")
print(type(response))

Response not in valid JSON format means that your response data is not in a valid JSON format.
It's not very clear what you want, but the URL you are requesting https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna does not return JSON formatted data, it returns a full html document (try opening the URL in your browser).
Therefore you cannot convert it to a JSON object.
JSON formatted data would look something like:
{ "name":"John", "age":30, "car":null }
If you would like to test your code you can use a URL from a placeholder service, i.e. https://jsonplaceholder.typicode.com/todos/1
A (GET) request to this URL will return JSON formatted placeholder data so you can test and experiment with your code.
You can learn more about JSON here: https://www.w3schools.com/js/js_json_intro.asp
Good luck!

Related

HTTP Request file in intellij, how to filter the first characters of the response to parse it

I am using the http client file to do requests in intellij (scratch.http), I want to parse the response and set a global variable. I saw here (https://www.jetbrains.com/help/idea/http-response-handling-examples.html#script-var-example) that I could use this:
POST https://httpbin.org/post
Content-Type: application/json
{
"token": "my-secret-token"
}
//Saving a variable
> {%
client.global.set("auth_token", response.body.json.token);
%}
But there is an issue, the endpoint I need to call returns the header Content-Type:application/json, but it's not really a json, because the response body starts with some characters, and then there is the json, so as an example:
)
]
}',
{
//normal json
}
how can I first slice the first characters of the response, and then parse the json to get the field that I want to set it as a global variable.
In postman I have this:
var jsonData = JSON.parse(responseBody.slice(5));
postman.setEnvironmentVariable("myField", jsonData.myField);
But I don't know how to do it with intellij http client.

Python requests for Google Book API. How to create url?

I'm using this API to search through books. I need to create a request with given parameters. When I use requests library and params argument it creates bad URL which gives me wrong response. Let's look at the examples:
import requests
params = {'q': "", 'inauthor': 'keyes', 'intitle': 'algernon'}
r = requests.get('https://www.googleapis.com/books/v1/volumes?', params=params)
print('URL', r.url)
The URL is https://www.googleapis.com/books/v1/volumes?q=&inauthor=keyes&intitle=algernon
Which works but gives a different response than when the link is as Working Wolumes tells.
Should be: https://www.googleapis.com/books/v1/volumes?q=inauthor:keyes+intitle:algernon
Documentation of requests tells only about params and separates them with &.
I'm looking for a library or any solution. Hopefully, I don't have to create them using e.g. f-strings
You need to create a parameter to send the url, the way you are doing it now is not what you wanted.
In this code you are saying that you need to send 3 query parameters, but that is not what you wanted. You actually want to send 1 parameter with a value.
import requests
params = {'q': "", 'inauthor': 'keyes', 'intitle': 'algernon'}
r = requests.get('https://www.googleapis.com/books/v1/volumes?', params=params)
print('URL', r.url)
try below code instead which is doing what you require:
import requests
params = {'inauthor': 'keyes', 'intitle': 'algernon'}
new_params = 'q='
new_params += '+'.join('{}:{}'.format(key, value) for key, value in params.items())
print(new_params)
r = requests.get('https://www.googleapis.com/books/v1/volumes?', params=new_params)
print('URL', r.url)

POST request with digest auth in R

I have some url, some raw data that looks like json and auth creds.
Everything together works great via postman, but now i need to automate it in R.
For example:
url1 = 'https://site.io/v2/passes/?values=true'
bod = paste0('{"values": [{"value": "0","label": "label1"},{"value": "5","label": "label2"}] }')
I've already tried this one
r <- POST(url1, body = bod, encode = "raw", httr::add_headers(.headers=headers)
, httr::authenticate("user", "password", type="digest"))
But it seems incorrect (and also getting 400 error)
I assume your bod is a text string. When you use encode="raw", you have to also specify the Content-encoding header if the host is expecting one (and in this case, I assume it's expecting "application/json":
headers <- c(headers, `Content-encoding`="application/json")
POST(url1, body=bod, encode="raw", add_headers(.headers=headers), ...)

How to get the Only success (200 ok) Response from Http Request Using Groovy script

Here i'm Facing problem with groovy script that.Want to extract the response data from the http request.When the Response data consisting of 200 as value then only extract that value_description and print value.
So here is the response what i am getting
{"value":"200","value_description":"pass"}
and code is
def response = new groovy.json.JsonSlurper().parse("200".equals(prev.getResponseData()))
means is there any possible that if value is 200 and than only print value description.using groovy script please tell me with simple code.
Not sure if this is what you mean, it's really hard to tell, but I think you mean:
import groovy.json.JsonSlurper
def response = new groovy.json.JsonSlurper().parseText(prev.responseData)
if (response.value == '200') {
println response.value_description
}

Meteor creating JSON object from json string

I need to create a JSON object from a json string. I tried JSON.parse(json-string) but I get error:
SyntaxError: Unexpected token o
But when I run the same string in Android JSONObject(json-string) I get a no errors
HTTP.get(url, function (error, response) {
if (response) {
var content = JSON.parse(response);
console.log(content);
Am I doing something wrong? Doesn't response contains the json-string and nothing else un-modified? Thanks
response is an object. You need to JSON.parse(response.content) or just response.data.
From official docs:
Contents of the result object:
statusCode Number
Numeric HTTP result status code, or null on error.
content String
The body of the HTTP response as a string.
data Object or null
If the response headers indicate JSON content, this contains
the body of the document parsed as a JSON object.
headers Object
A dictionary of HTTP headers from the response.

Resources