How to send post request for apache durid in python - python-requests

I want to send a post requests with SQL query to a druid API. I used requests package to make the request:
payload = {
"query": "select count(1) from table1",
"resultFormat": "array",
"header": True,
"typesHeader": True,
"sqlTypesHeader": True,
"context": {
"somekeys":"somevalues"
}
}
druidURL = "someurl:8888/durid/v2/sql"
x = requests.post(druidUrl,json=payload)
The only result I got is code:405.When I inspect the network in Chrome when the POST request succussed with the result on the druid console, the URL and Payload are exactly the same except there is an additional property called Remote Address.

You have a typo in the URL ("durid"):
import requests
payload = {
"query": "select 1+1",
"resultFormat": "array",
"header": True,
"typesHeader": True,
"sqlTypesHeader": True,
"context": {
"somekeys":"somevalues"
}
}
druidUrl = "http://localhost:8888/druid/v2/sql"
x = requests.post(druidUrl, json=payload)
print(x)

Related

MirageJS Error response not causing an error

Following the docs I've set up this handler inside routes():
this.put(
'/admin/features/error/environment/test',
// #ts-ignore
() => new Response(500, {}, { errors: ['The database went on vacation'] }),
);
Mirage does receive what I've set, sort of. Here is its response, from the browser console logs. Note that it's not an error although the 500 shows up in _bodyInit:
{
"type": "default",
"status": 200,
"ok": true,
"statusText": "",
"headers": {
"map": {
"content-type": "text/plain;charset=UTF-8"
}
},
"url": "",
"bodyUsed": false,
"_bodyInit": 500,
"_bodyText": "[object Number]"
}
Note that I need ts-ignore which is probably a clue. TS complains that new Response expects 0-2 arguments but got 3.
Try importing the Mirage Response class:
import { Response } from 'miragejs';
Otherwise, Response refers to a Fetch API Response object. This explains the type checking error and the unexpected behavior when calling the route.
After adding the import you can remove #ts-ignore and requests to the route should fail with status code 500.

Firestore HTTP Insomnia query : Stream error in the HTTP/2 framing layer

I'm trying to query my Firestore database using an HTTP query via the Insomnia API:
https://firestore.googleapis.com/v1/projects/typebot/databases/(default)/documents/users
with this body:
{
"structuredQuery": {
"from": [
{
"collectionId": "users"
}
],
"where": {
"fieldFilter": {
"field": {
"fieldPath": "email"
},
"op": "EQUAL",
"value": {
"stringValue": "email#test.com"
}
}
}
}
}
And I get the following error:
HTTP query: Stream error in the HTTP/2 framing layer
Any idea what's wrong?
May try to change "GET" to "POST"
I had a similar problem performing a GET request:
GET https://us-central1-my-project-id.cloudfunctions.net/getUsers HTTP/1.1
content-type: application/json
{
"minAge": "18"
}
against an endpoint defined by this Firestore HTTP Cloud Function:
exports.getUsers = functions.https.onRequest(async (req, res) => {
// find matching users by age limit.
const ageLimit = req.body.age;
...
});
Turns out, based on this other SO post, that a request body with GET does not have any meaning in the sense that the HTTP spec recommends that the "message-body SHOULD be ignored when handling the request" (presumably, by the server, and that the Firestore server implements this behavior). Oddly, I didn't catch this issue running the exact same function locally with the Functions emulator, so it is likely the local server ignores this recommendation.
To fix the issue, I changed my function to parse the query params instead of a request body:
exports.getUsers = functions.https.onRequest(async (req, res) => {
// find matching users by age limit.
const ageLimit = req.query.age; // extract the age as a query param
...
});
and the request:
GET https://us-central1-my-project-id.cloudfunctions.net/getUsers?age=18

WebApi returning serialized HttpResponseMessage instead of text/html

I can't for the life of me figure this out because if I start a fresh WebApi project this works just fine. But in my existing application, it isn't working the same way. I'm dynamically building HTML and returning. Code is below:
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(some html goes here)
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
The response from that code is (200 OK):
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/html"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
Some of the configuration that might be relevant:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
config.Formatters.Remove(config.Formatters.XmlFormatter);
The request is configured to accept the standard content types.
I've read some things about creating a custom formatter but I think that isn't necessary seeing that I'm able to return HTML just fine in a blank Web API project. I'm thinking something with the middleware or something else in the pipeline is intercepting the object and serializing.
.NET 4.6
NuGet Packges: Microsoft.AspNet.WebApi.* are set to 5.2.5.
This is an issue I'm experiencing in my local environment.
As always, any help is appreciated. Thanks!

SWIFT 3 Post request with VIEWSTATE

I need to log in example.com/mobile/shared/default.aspx by using POST request
How do i get current ViewState and sending it after?
That is what i tried
(Alamofire)
func webRequest()
{
let parameters: Parameters = [
"name": "name",
"password": "password",
"enter": "Enter",
]
Alamofire.request("http://example.ru/mobile/shared/default.aspx", parameters: parameters).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
}
I'm using Alamofire like this:
let Parameters = [
"name": "name",
"password": "password",
"enter": "Enter"]
var json : JSON = nil
Alamofire.request(URLString, method: method, parameters: Parameters)
.responseJSON { response in
switch response.result {
case .success(let data):
json = JSON(data)
print(json)
case .failure(let error):
print("Request failed with error: \(error)")
}
}
}
So after that you can parse your json like this for example:
if json != nil {
let token = json["token"].stringValue
}
But all of that depend of your server request params, and request response from your server.
Hope I helped you, Peace

Can't convert successful Postman call to Ionic-Native HTTP.post [Ionic 2 ts]

I'm in the process of building an Ionic 2 (ts) app that will send a REST call to the OCR.space API. Going through their examples, I'm able to send a a Base64Image via HTTP.post, but when attempting to send a file via HTTP.Post, I'm met with:
{"ParsedResults":null,"OCRExitCode":0,"IsErroredOnProcessing":false,"ErrorMessage":["Parameter
name 'file' is invalid. Valid parameters:
apikey,url,language,isoverlayrequired,base64image"],"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1"}
I'm guessing it's my formatting of my post request:
HTTP.post('http://api.ocr.space/parse/image',
{ "apikey":"helloworld", "language":"eng", "isOverlayRequired":"false", "file": "asssets/img/test2.pdf" }, {})
.then(data => {
console.log("HTTP entered");
let result = JSON.parse(data.data); // data received by server
console.log(data.data);
})
.catch(error => {
console.log(error.error); // error message as string
});
And I'm guessing this because I'm able to send PDF files successfully via postman like below: my successful postman request
So - I'd love some help figuring out how to send this HTTP.post request successfully or convert the code I can get from postman to successful ionic-native syntax.
var form = new FormData();
form.append("apikey", "541496f13e88957");
form.append("language", "eng");
form.append("isOverlayRequired", "false");
form.append("file", "1page.pdf");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.ocr.space/parse/image",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "1aea47d5-a0eb-7768-5fa6-60c4cd76d453"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
I appreciate any and all help!
I'm using cordova-plugin-file-transfer to send file instead of Base64Image or for Ionic 2 using ionic-native with the examples

Resources