I got a problem which I dont understand.
I try to post data to my API in a form using the following code
formSubmit() {
const req =this.http.post('http://[ip]/api/login', {
id: '7',
username: 'PostTest',
password: 'studp123lan',
matrikelnr: 'winf303666',
email: 'winf303666#example.de',
email_verified: '1'
})
.subscribe(
res => {
console.log(res);
},
err => {
console.log("Error occured");
}
When I inspect it in the Chrome Developter tools, this is what I get:
Failed to load http://[ip]/api/login: Response for preflight has
invalid HTTP status code 404
register.component.ts:42 Error occured
And this is what I get in the network tab:
General:
Request URL:http://[ip]/api/login
Request Method:OPTIONS
Status Code:404 Not Found
Remote Address:[ip]:80
Referrer Policy:no-referrer-when-downgrade
Response Header:
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Content-Length:0
Date:Thu, 21 Dec 2017 09:00:35 GMT
Server:Kestrel
X-Powered-By:ASP.NET
Request Header:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:[ip]
Origin:http://localhost:4200
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36
Somehow, this doesn't work. But when I Post the same data via Postman Post Request to the same URL, it works like a charm.
Can anyone explain and help?
Thanks.
404 is a page not found error : this means your endpoint isn't available at this address.
Sure, you make a postman call and it works : but did you create your postman call by hand, or used the interceptor to make it ? (The interceptor is a Chomr plugin that allows you to register all calls made by Chrome, into postman).
There must be something you have forgotten. Could you post your postman call, and if you can, try with the interceptor ?
Related
I am making a python module that interacts with Carousell using the requests module. Now I am trying to send a post request with a JSON payload, but I keep getting HTTP error code 422(UNPROCESSABLE ENTITY). I don't know what's wrong with my JSON payload, python dict(before it's converted to JSON) or perhaps I am missing something in my request headers.
I tried taking the raw json string(from the POST request that I captured using Chrome dev tools) converting it dict and copy that dict(printed out) and try to use it in the program. It didn't work.
login_session = requests.session()
login_session.headers.update({"DNT":"1", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36", "Origin":"https://sg.carousell.com"})
login_payload = {'requests': {'g0': {'resource': 'sso', 'operation': 'create', 'params': {'loginToken': cookies["login-token"]}, 'body': {}}}, 'context': {'_csrf': cookies["_csrf"]}}
login_cookies = {"__cfduid": cookies["__cfduid"], "_csrf": cookies["_csrf"], "gtkprId": cookies["gtkprId"], "login-token": cookies["login-token"], "redirect":"redirect"}
login_headers = {'accept':'*/*','accept-encoding':'gzip, deflate, br','accept-language':'en-GB,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-US;q=0.6', 'x-requested-with': 'XMLHttpRequest', 'content-type': 'application/json'}
login_data = login_session.post(query_url, cookies=login_cookies, data=json.dumps(login_payload), headers=login_header)
Heres the output from debugging logger
DEBUG:urllib3.connectionpool:https://sg.carousell.com:443 "POST /ui/iso?_csrf=TNZTMZpBdQYgRFFouCF4ELVB HTTP/1.1" 422 0
Edit:
Heres the JSON payload which was sent to the server. I am trying to replicate it.
{"requests":{"g0":{"resource":"sso","operation":"create","params":{"loginToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTg1MDQyMjQsImlzcyI6ImxvZ2luLmNhcm91c2VsbC5jb20iLCJzc29pZCI6IkRacG1rd1l1SXAxdDF5U3A2M1RXWExPUTJnWmRFRzBOSHd3d0ZGSm9PSkFvVFFOdGFyNWt0MDMzNm5EVHRudHoiLCJ1c2VyaWQiOiIxNDczMjI3NCJ9.x7YxdLLk1ID6_jWy4trtLzbrPnZZ0eI7g_cQN1BilF8"},"body":{}}},"context":{"_csrf":"hPPhgajp-1GMLSbgjZBNBD7z2EGPVGCuA_mU"}}
Note that login token and _csrf are data from the cookies.
The requirement is to make a POST request to a particular URL with a given JSON payload. The URL will only respond if the payload is correct and the request is via POST.
This is my code:
request1 = HTTPRequest()
control = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()
control.setProxyServer("proxy.example.com", 1234)
payload = JSONObject({
"uaaURL": "https://com-example.something.com",
"sampleID": "admin",
"sampleSecret": "password",
"sampleID2": "example-sample-el",
"sampleSecret2": "ssenjsoemal/+11=",
"username": "test",
"someAttributes": {
"Groups": [
"example_com-abc"
],
"attribute": [
"value1"
]
}
})
payload = str(payload)
url = "https://example-something.com:6443/getvalues"
headers = [
NVPair('Content-Type', 'application/json'),
NVPair('Charset', 'UTF-8'),]
class TestRunner:
def __call__(self):
result = request1.POST(url, payload, headers)
print payload, headers
Now the issue with this is that my POST request gives me a 403 forbidden. However, when I use the same payload and send the request using DHC, it gives me a 200. So I'm sure of the payload and the link I'm connecting to. The proxy also I've tested in another script and works fine. Besides, if the proxy didn't work, I wouldn't get a 403 either.
Lastly, I'm parsing it as a string because POST requires the second argument to be string that it will internally convert into byte[].
I'm really not able to understand what's happening so any insight would be immensely helpful. Thanks in advance
EDIT: Fiddler's catch of DHC's Request
POST https://example-something.com:6443/getvalues HTTP/1.1
Host: example-something.com:6444
Connection: keep-alive
Content-Length: 688
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
{
"uaaURL": "https://com-example.something.com",
"sampleID": "admin",
"sampleSecret": "password",
"sampleID2": "example-sample-el",
"sampleSecret2": "ssenjsoemal/+11=",
"username": "test",
"someAttributes": {
"Groups": [
"example_com-abc"
],
"attribute": [
"value1"
]
}
}
I even edited my Grinder request headers to so
headers = (
NVPair('Content-Type', 'application/json'),
NVPair('Charset','UTF-8'),
NVPair('Accept', '*/*'),
NVPair('Accept-Encoding', 'gzip, deflate, br'),
NVPair('Accept-Language', 'en-US,en;q=0.8'),
NVPair('Connection', 'keep-alive'),
)
Best guess? You are likely missing a header with some credential information to pass a proxy/firewall/access gate to the application. Your REST Client, DHC, likely is passing this additional data but grinder is not. Grab a proxy (Fiddler, Charles, etc...) and check out the handshake from DHC to your destination and then match that with Grinder. My guess is you will find the delta.
The issue was that Grinder was not able to access the port. It had nothing to do with the JSON. The URL, which runs on port 6443 was the problem and Grinder couldn't access that (don't know why). I changed my URL itself to run on the default port 8080 and instantly my script worked. Thanks for the help!
I have runing bitcoind on ubuntu. bitcoin-cli works fine. I can not get working json rpc protocol
bitcoin.conf file:
testnet=0
rpcuser="bitcoinrpc"
rpcpassword="xxxxx"
rpcport=8332
rpcallowip="*"
server=1
http post request with url='http://bitcoinrpc:xxxxx#127.0.0.1:8332/' fails with 401 error.
request headers:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6,de;q=0.4,sr;q=0.2
Authorization:Basic Yml0Y29pbnJwYzp4eHh4eA==
Cache-Control:no-cache
Connection:keep-alive
Content-Length:53
Content-Type:text/plain
DNT:1
Host:127.0.0.1:8332
Origin:chrome-extension://fhjcajmcbmldlhcimfajhfbgofnpcjmb
Pragma:no-cache
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36
request post payload:
{jsonrpc: "2.0", method: "getinfo", params: []}
What is correct way for bitcoind json rpc autentification?
For future googlers: a possible problem is that the password should not contain the pound sign (#) as this is treated as a comment!
i tried to cache a json response from an api request using the Etag.
I'm calling something like this http://localhost:3000/api/config and getting:
Response Headers:
Cache-Control:public, max-age=31557600
Connection:keep-alive
Content-Length:11
Content-Type:application/json; charset=utf-8
Date:Wed, 13 May 2015 11:41:52 GMT
ETag:"94d52736bcd99b1ac771f13b1bbdf622"
X-Powered-By:Express
Resonse: {id: 1}
I expected the browser to cache the response and to send the Etag with the next request triggert by "f5". But this isn't the case.
Request Headers 2nd request:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:3000
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
So is it impossible to cache pure json response, getting by a direct api requests? Or do i miss something.
The api is an node js test implemantation done with express:
router.get('/config', function(req, res) {
var eTag = crypto.createHash('md5').update(JSON.stringify(config)).digest('hex');
res.setHeader('ETag', '"' + eTag + '"');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'public, max-age=31557600');
});
Testet with chrom(42.x) and firefox(37.x)
Thx for response.
Hi this code seems to work for me:
router.get('/config', function(req, res) {
var eTag = crypto.createHash('md5').update(JSON.stringify(config)).digest('hex');
if (req.headers['if-none-match'] && req.headers['if-none-match'] === '"' + eTag + '"') {
res.status(304);
res.end();
} else {
res.setHeader('ETag', '"' + eTag + '"');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'public, max-age=31557600');
res.send(JSON.stringify(config));
}
});
Calling the api using browser url bar http://localhost:3000/api/config
Looks like you might be using chrome.
Chrome should include the following header in the request after "f5":
If-None-Match:"94d52736bcd99b1ac771f13b1bbdf622"
If you don't see this, check the chrome settings / General and make sure that "Disable cache (while DevTools is open) is not checked:
Using jQuery we can use the ifModified option:
$.ajax({
type: "GET",
ifModified: true,
url: "http://localhost:3000/api/config"
}).then(function(data) {
. . .
});
I'm new to JSONP and I'm trying to access a cross domain file hosted on my webserver.
UPDATE: I found another solution to fix my issue: I used CORS for JQUERY of http://www.html5rocks.com/en/tutorials/cors/
Here is my js file:
var url = "myip:port/pgif.jsonp?callback=?"
var url2 = "http://puppygifs.tumblr.com/api/read/json?callback=?";
$.getJSON(url2, {
tags: "jquery",
tagmode: "any",
format: "json"
}, function (data) {
console.log("d= ", data);
});
}
);
When I try with url2, it works well but when I try with url to access my file on my webserver it doesn't.
Here is what I can see in the Network tab of Chrome Dev tools with url2 which works:
Request URL:http://puppygifs.tumblr.com/api/read/json?callback=jQuery19107969533477444202_1386114768597&tags=jquery&tagmode=any&format=json&_=1386114768598
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,fr-FR;q=0.8,fr;q=0.6
Cookie:__utma=189990958.626465526.1385955515.1385955515.1385955515.1; __utmc=189990958; __utmz=189990958.1385955515.1.1.utmcsr=sarajchipps.com|utmccn=(referral)|utmcmd=referral|utmcct=/2011/01/using-jquery-ajax-calls-in-a-chrome-plugin.html
Host:puppygifs.tumblr.com
Proxy-Authorization:Negotiate TlRMTVNTUAADAAAAGAAYAIQAAABiAWIBnAAAAAwADABYAAAAEAAQAGQAAAAQABAAdAAAABAAEAD+AQAAFYKI4gYBsR0AAAAPwICK4LzPlRZWoe1WgJ+erkYATwBYAFQARQBMAGIAYQBzAHQAaQBkAGUAZgBIAFEAMAAyADcANAAzADUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+t3F+qb2rUYkHj2KQg3XtAEBAAAAAAAAanrqyYLwzgG/iL4+KqlreQAAAAACAAwARgBPAFgAVABFAEwAAQAQAFMAWQBEAFQATQBHADAAMQAEACIAZQBuAHQALgBmAG8AeAB0AGUAbAAuAGMAbwBtAC4AYQB1AAMANABTAFkARABUAE0ARwAwADEALgBlAG4AdAAuAGYAbwB4AHQAZQBsAC4AYwBvAG0ALgBhAHUABQAiAGUAbgB0AC4AZgBvAHgAdABlAGwALgBjAG8AbQAuAGEAdQAHAAgAanrqyYLwzgEGAAQAAgAAAAgAMAAwAAAAAAAAAAAAAAAAIAAA+jYk8RidWxnk5XJtyRLc7j6jPZX/e7BWswB5GZeS2JYKABAAAAAAAAAAAAAAAAAAAAAAAAkAIgBIAFQAVABQAC8AMQAwAC4AMgAwAC4AMQA2AC4AMQAwADAAAAAAAAAAAAAAAAAA2S3kxHzp0FstuiERqIRwqQ==
Proxy-Connection:keep-alive
Referer:http://ip:port/MyApp/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery19107969533477444202_1386114768597
tags:jquery
tagmode:any
format:json
_:1386114768598
Response Headersview source
Cache-Control:max-age=900
Connection:Keep-Alive
Content-Type:text/javascript
Date:Tue, 03 Dec 2013 23:52:57 GMT
Last-Modified:Sun, 03 Feb 2013 12:02:52 GMT
P3P:CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Proxy-Connection:Keep-Alive
Transfer-Encoding:chunked
Vary:X-UA-Device
Via:1.1 SYDTMG01
X-Cache-Metadata:
X-Robots-Tag:noindex
X-UA-Device:desktop
Here is what I can see in the Network tab of Chrome Dev tools with url which doesnt work:
Request URL:http://ip:port/pgif.jsonp?callback=jQuery1910627754604909569_1386116200762&tags=jquery&tagmode=any&format=json&_=1386116200763
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,fr-FR;q=0.8,fr;q=0.6
Connection:keep-alive
Host:ip:port
Referer:http://ip:port/MyApp/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery1910627754604909569_1386116200762
tags:jquery
tagmode:any
format:json
_:1386116200763
Response Headersview source
Accept-Ranges:bytes
Access-Control-Allow-Origin:*
Content-Length:6616
Content-Type:application/javascript
Date:Thu, 01 Jan 1970 02:02:33 GMT
ETag:"68647623"
Last-Modified:Thu, 01 Jan 1970 01:23:16 GMT
Server:lighttpd/1.4.30
So as you can see, in the last one, I added Access-Control-Allow-Origin:* in the config of my lighttpd webserver, which doesnt improve the situation.
so I dont really understand, I receive an Ok answer from the webserver but my success function is never called.
Here is the JSONP file I'm trying to access on my webserver:
var tumblr_api_read = {"tumblelog":{"title":"puppy gifs"},"posts-start":0,"posts-total":"18"}
So, I didn't managed to make the JSONP work with my web server but I found a different way to solve my problem:
I followed the "CORS from JQuery" from html5rocks.com/en/tutorials/cors
And I'm now able to read my file sitting on my webserver.
It seems like if I want to edit my file, I would need to configure my web server to be able to handle the preflight request. As explained in x3dom.readthedocs.org/en/latest/notes/cors.html