Handling Http status code 302 Moved Temporarily - http-status-code-302

It can be said that when I receive a status code 302 and in the response a Location header with another URL, it means that I should make another request in this other URL?
Sending Http POST request to https://customer-test.com/order/checkout?id=491
Response headers
Status code HTTP/1.1 302 Found
Response headers:
Header name: Content-Type, header value: text/html; charset=utf-8
Header name: Transfer-Encoding, header value: chunked
Header name: Status, header value: 302 Found
Header name: Cache-Control, header value: no-cache
Header name: Referrer-Policy, header value: strict-origin-when-cross-origin
Header name: X-Robots-Tag, header value: none
Header name: X-Permitted-Cross-Domain-Policies, header value: none
Header name: X-XSS-Protection, header value: 1; mode=block
Header name: X-Request-Id, header value: af014ce9-3e67-4aa7-9dc3-cfaffe2b301a
Header name: Location, header value: https://another.url.com/sessions/new
Header name: X-Download-Options, header value: noopen
Header name: X-Runtime, header value: 0.122752
Header name: X-Frame-Options, header value: SAMEORIGIN
Header name: X-Content-Type-Options, header value: nosniff
Header name: Date, header value: Tue, 20 Jul 2021 16:35:16 GMT
Header name: Set-Cookie, header value: _customer_session=59939fa7ad350971e4799114897sdf5; path=/; expires=Wed, 21 Jul 2021 16:35:16 -0000; secure; HttpOnly; SameSite=None
Header name: Strict-Transport-Security, header value: max-age=16070400
Header name: Front-End-Https, header value: onenter code here
Afterwards, I tried to get the location URL and submit another HTTP POST, but I get a 400 status code.
Disparando POST request para https://sanofi-test.coupahost.com/sessions/new
Sending Http POST request para https://customer-test.com/sessions/new
Status code HTTP/1.1 400 Bad Request
Response headers:
Header name: Content-Type, header value: text/html
Header name: Transfer-Encoding, header value: chunked
Header name: Status, header value: 400 Bad Request
Header name: Cache-Control, header value: no-cache
Header name: Referrer-Policy, header value: strict-origin-when-cross-origin
Header name: X-Robots-Tag, header value: none
Header name: X-Permitted-Cross-Domain-Policies, header value: none
Header name: X-XSS-Protection, header value: 1; mode=block
Header name: X-Request-Id, header value: 1c8c4c6b-bd94-4b98-b83e-974569276e4d
Header name: X-Download-Options, header value: noopen
Header name: X-Runtime, header value: 0.102914
Header name: X-Frame-Options, header value: SAMEORIGIN
Header name: X-Content-Type-Options, header value: nosniff
Header name: Date, header value: Tue, 20 Jul 2021 16:35:17 GMT
Sorry, I'm new and I don't understand the situation.

Related

Send MultiValueMap as MultiPartFormData in Feign Client

I am trying to convert the below kotlin code from RestTemplate to Feign client. The rest template code sends multiValueMap as request with content-type header multipart/form-data and consumes JSON object as response.
RestTemplate Code:
var headers = HttpHeaders()
headers.contentType = MediaType.MULTIPART_FORM_DATA
headers.add("custom-header", "value")
val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
body.add("field1", "value1")
body.add("field2", "value2")
val requestEntity = HttpEntity(body, headers)
return restTemplate.postForEntity("https://enmf7tx8y37x.x.pipedream.net/", requestEntity, Object::class.java)
In this case the request is sent as below:
Headers:
Host: enmf7tx8y37x.x.pipedream.net
X-Amzn-Trace-Id: Root=1-6303ecb2-19a833a044ab3bf83f74f256
Content-Length: 342
Accept: application/xml, text/xml, application/json, application/*+xml, application/*+json
Content-Type: multipart/form-data;boundary=_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
custom-header: value
X-B3-TraceId: a67561ec329f9a16
X-B3-SpanId: a6cc94e403bfe318
X-B3-ParentSpanId: a67561ec329f9a16
X-B3-Sampled: 1
User-Agent: Apache-HttpClient/4.5.13 (Java/17.0.3)
Accept-Encoding: gzip,deflate
Body:
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
Content-Disposition: form-data; name="field1"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
value1
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj
Content-Disposition: form-data; name="field2"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
value2
--_MtEGFIF4XK_aOU8QsXstQuCliV1-llj--
I tried to do the same in Feign client:
code:
/*val headers = HttpHeaders()
headers.contentType = MediaType.MULTIPART_FORM_DATA
headers.add("custom-header", "value")*/
val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
body.add("field1", "value1")
body.add("field2", "value2")
val result = testClient.test("value", body)
Feign Client:
#FeignClient(
value = "testClient",
url = "https://enmf7tx8y37x.x.pipedream.net/"
)
interface TestClient {
#PostMapping(
consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun test(
#RequestHeader(value = "custom-header") customHeader: String,
#RequestPart("request") request: MultiValueMap<String, Any>
): ResponseEntity<Object>
}
The header are fine but no value present in the body.
Header:
Host: enmf7tx8y37x.x.pipedream.net
X-Amzn-Trace-Id: Root=1-6303ef0f-78c869881a5b27d0707eab9e
Content-Length: 17
Accept: application/json
Authorization: Basic aHlwb2xhYjp0ZXN0c211cmY=
Content-Type: multipart/form-data; charset=UTF-8; boundary=182c75dd399
custom-header: value
X-B3-TraceId: 2989eb4f12e3d417
X-B3-SpanId: 23414bcdf365784c
X-B3-ParentSpanId: 2989eb4f12e3d417
X-B3-Sampled: 1
User-Agent: Java/17.0.3
Body:
--182c75dd399--
I had to add consumes value as multipart/form-data instead of json to get the right header values for Accept and Content-Type.
How can I populate the request using Feign client? If the #RequestPart is String then the value is sent in the body but any other data type like multiValueMap, byteArray, etc were not working
In feign client you cannot use MultiValueMap directly. You have to use MultipartFile datatype for bytearray and for the remaining metadata fields you need to mention each one as a separate argument in the method. Then FeignClient will generate the same request like the one you showed when using RestTemplate.
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.multipart.MultipartFile
#FeignClient(
value = "testClient",
url = "https://enmf7tx8y37x.x.pipedream.net/"
)
interface TestClient {
#PostMapping(
consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun test(
#RequestHeader(value = "custom-header") customHeader: String,
#RequestPart(name = "file") file: MultipartFile,
#RequestPart(name = "field1") field1: String
): ResponseEntity<Object>
}
code sample for How to create MultiPartFile:
import org.springframework.mock.web.MockMultipartFile
//val multipartFile: MultipartFile = MockMultipartFile("filename", byteArray)
val multipartFile: MultipartFile = MockMultipartFile("filename", "filename", "content type like application/pdf", byteArray)

Odata Controller: Content () method behaving unexpected when pass C# object entity as input

I am trying to pass an C# object in Odatacontroler Content method:
var resp = Content(HttpStatusCode.OK, vResult );
Where vResult is a C# object with two fields (List Messaages; bool IsValidEntity) .
At client when I tried getting response bellow unexpected response coming:
Id = 0x00000003, Status = RanToCompletion, Method = "{null}", Result =
"StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content:
System.Net.Http.StringContent, Headers:\r\n{\r\n Pragma: no-cache\r\n
Cache-Control: no-cache\r\n Date: Wed, 06 Jan 2016 07:04:25 GMT\r\n
Server: Microsoft-IIS/7.5\r\n X-AspNet-Version: 4.0.30319\r\n
X-Powered-By: ASP.NET\r\n Content-Type: application/json;
odata.metadata=minimal\r\n Content-Length: 97\r\n}"
For details below can be referenced:
Odata Controller: How to convert Odata response to C# object at client

How to describe this POST JSON request body in OpenAPI (Swagger)?

I have a POST request that uses the following JSON request body. How can I describe this request body using OpenAPI (Swagger)?
{
"testapi":{
"testapiContext":{
"messageId":"kkkk8",
"messageDateTime":"2014-08-17T14:07:30+0530"
},
"testapiBody":{
"cameraServiceRq":{
"osType":"android",
"deviceType":"samsung555"
}
}
}
}
So far I tried the following, but I'm stuck at defining the body schema.
swagger: "2.0"
info:
version: 1.0.0
title: get camera
license:
name: MIT
host: localhost
basePath: /test/service
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/getCameraParameters:
post:
summary: Create new parameters
operationId: createnew
consumes:
- application/json
- application/xml
produces:
- application/json
- application/xml
parameters:
- name: pet
in: body
description: The pet JSON you want to post
schema: # <--- What do I write here?
required: true
responses:
200:
description: "200 response"
examples:
application/json:
{
"status": "Success"
}
I want to define the input body inline, as a sample for documentation.
I made it work with:
post:
consumes:
- application/json
produces:
- application/json
- text/xml
- text/html
parameters:
- name: body
in: body
required: true
schema:
# Body schema with atomic property examples
type: object
properties:
testapi:
type: object
properties:
messageId:
type: string
example: kkkk8
messageDateTime:
type: string
example: '2014-08-17T14:07:30+0530'
testapiBody:
type: object
properties:
cameraServiceRq:
type: object
properties:
osType:
type: string
example: android
deviceType:
type: string
example: samsung555
# Alternatively, we can use a schema-level example
example:
testapi:
testapiContext:
messageId: kkkk8
messageDateTime: '2014-08-17T14:07:30+0530'
testapiBody:
cameraServiceRq:
osType: android
deviceType: samsung555
The most readable way to include a multi line scalar into YAML is by using the block literal style. This requires you to change your JSON example only by using indentation (which will be removed if you retrieve the value for the key):
.
.
produces:
- application/json
example: |
{
"testapi": {
"testapiContext": {
"messageId": "kkkk8",
"messageDateTime": "2014-08-17T14:07:30+0530"
},
"testapiBody": {
"cameraServiceRq": {
"osType": "android",
"deviceType": "samsung555"
}
}
}
}
paths:
/getCameraParameters:
.
.
(for clarity you can put an extra newline or two before the paths scalar key, they get clipped by default on the literal block style scalars.
openapi version >= 3.0.0 allows for the use of a requestBody which would allow for request body definitions outside of parameters.
In your case it would look something like this:
...
requestBody:
description: The pet JSON you want to post
required: true
content:
application/json:
schema:
type: object
properties:
testapi:
type: object
properties:
messageId:
type: string
example: kkkk8
messageDateTime:
type: string
example: '2014-08-17T14:07:30+0530'
testapiBody:
type: object
properties:
cameraServiceRq:
type: object
properties:
osType:
type: string
example: android
deviceType:
type: string
example: samsung555
example:
testapi:
testapiContext:
messageId: kkkk8
messageDateTime: '2014-08-17T14:07:30+0530'
testapiBody:
cameraServiceRq:
osType: android
deviceType: samsung555
...

incompatibility when running dart rpc and shelf (with shelf_rpc) related to headers which are lists (and not Strings)

incompatibility when running dart rpc and shelf (with shelf_rpc)
related to headers which are lists (and not Strings).
It seems that there is an incompatibility when running dart rpc and shelf (with shelf_rpc)
related to headers which are lists (and not Strings).
Error thrown is ( for shelf[0.5.7], shelf_rpc[0.0.3], rpc[0.4.2]: ):
Error thrown by handler.
type 'List' is not a subtype of type 'String' of 'value'.
package:collection/src/canonicalized_map.dart 66:30 CanonicalizedMap.[]=
package:collection/src/canonicalized_map.dart 71:39 CanonicalizedMap.addAll.<fn>
dart:collection _CompactLinkedHashMap.forEach
package:collection/src/canonicalized_map.dart 71:18 CanonicalizedMap.addAll
package:collection/src/canonicalized_map.dart 57:11 CanonicalizedMap.CanonicalizedMap.from
package:shelf/src/response.dart 215:9 Response.Response
package:shelf_rpc/shelf_rpc.dart 18:24 createRpcHandler.<fn>.<fn>
A workaround around this problem is to change shelf_rpc.dart to replace the Lists by Strings:
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:shelf/shelf.dart";
import "package:rpc/rpc.dart";
/// Creates a Shelf [Handler] that translates Shelf [Request]s to rpc's
/// [HttpApiRequest] executes the request on the given [ApiServer] and then
/// translates the returned rpc's [HttpApiResponse] to a Shelf [Response].
Handler createRpcHandler(ApiServer apiServer) {
return (Request request) {
try {
var apiRequest = new HttpApiRequest(request.method, request.requestedUri,
request.headers, request.read());
return apiServer.handleHttpApiRequest(apiRequest).then(
(apiResponse) {
// EXTRA: print and work-around
printHeaders(apiResponse.headers, true);
printHeaders(apiResponse.headers, false);
// EXTRA <end>
return new Response(apiResponse.status, body: apiResponse.body,
headers: apiResponse.headers);
});
} catch (e) {
// Should never happen since the apiServer.handleHttpRequest method
// always returns a response.
return new Response.internalServerError(body: e.toString());
}
};
}
// EXTRA WORKAROUND: print headers & replace Lists by Strings
printHeaders(Map headers, bool replaceListsBytStrings) {
print('--HEADERS start---');
headers.forEach(
(key, value) {
print('key: $key - value: $value - type: ${value.runtimeType}');
if ( (replaceListsBytStrings) && (value is List) ) {
String str = value.toString().substring(1, value.toString().length-1);
headers[key] = str;
}
});
print('--HEADERS end---');
}
Output:
--HEADERS start---
key: content-type - value: application/json; charset=utf-8 - type: String
key: cache-control - value: no-cache, no-store, must-revalidate - type: String
key: pragma - value: no-cache - type: String
key: expires - value: 0 - type: String
key: access-control-allow-credentials - value: true - type: String
key: access-control-allow-origin - value: * - type: String
key: allow - value: [GET] - type: List
key: access-control-allow-methods - value: [GET] - type: List
key: access-control-allow-headers - value: origin, x-requested-with, content-type, accept - type: String
key: Access-Control-Allow-Headers - value: null,Authorization, content-type - type: String
--HEADERS end---
--HEADERS start---
key: content-type - value: application/json; charset=utf-8 - type: String
key: cache-control - value: no-cache, no-store, must-revalidate - type: String
key: pragma - value: no-cache - type: String
key: expires - value: 0 - type: String
key: access-control-allow-credentials - value: true - type: String
key: access-control-allow-origin - value: * - type: String
key: allow - value: GET - type: String
key: access-control-allow-methods - value: GET - type: String
key: access-control-allow-headers - value: origin, x-requested-with, content-type, accept - type: String
key: Access-Control-Allow-Headers - value: null,Authorization, content-type - type: String
--HEADERS end---
This should be fixed in the latest version of the Dart RPC package (v0.4.3). Please try it out and let me know how it works.
/gustav

Making a post request to pastebin returns a 302 - Moved Temporarily response

I am doing a simple post using GM_xmlhttpRequest to post some text to pastebin. This is my code
GM_xmlhttpRequest({
method: "POST",
url: "http://pastebin.com/post.php",
data: "api_dev_key=5c3***********************073a&api_option=paste&api_paste_code="+encodeURI(photo_url)+"&api_user_key=5******************************f&api_paste_name=cornered&api_paste_private=2&api_paste_expire_date=1W",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
onload: function(response) {
alert("posted " + response);
}
});
I am getting no response in the script. To check if the post request itself is working, I used http://requestmaker.com/ and it shows that the POST request is returning a 302 response
Request Headers Sent:
POST /post.php HTTP/1.1
Host: pastebin.com
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 152
Response Headers:
HTTP/1.1 302 Moved Temporarily
Date: Wed, 10 Dec 2014 15:27:20 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d61fd0544ec1b0c284ba908367557bc7f1418225240; expires=Thu, 10-Dec- 15 15:27:20 GMT; path=/; domain=.pastebin.com; HttpOnly
X-Powered-By: PHP/5.5.5
Set-Cookie: cookie_key=1; expires=Wed, 07-Jan-2015 15:27:20 GMT; Max-Age=2419200; path=/; domain=.pastebin.com
Set-Cookie: realuser=1; expires=Thu, 11-Dec-2014 15:27:20 GMT; Max-Age=86400; path=/
location: /index.php?e=2
Vary: Accept-Encoding
Server: cloudflare-nginx
CF-RAY: 196a7749d5c713e9-LAX
Send your request to http://pastebin.com/api/api_post.php instead of http://pastebin.com/post.php.

Resources