HTTPS SOAP request outputs junk characters in response headers - ruby

To make SOAP HTTPS call I write this method in jRuby.
def run host, port, path, soap_request_file,password,protocol = "HTTP"
soapMsg = ""
request = ""
soap_request_file = Expertus::TestDataFile::get_file_relative_to_script(soap_request_file)
soapMsg = IO.read(soap_request_file)
request = "POST #{path} #{protocol}/1.1\r\n"
request = request + "Content-Type: text/xml;charset=UTF-8\r\n"
request = request + "Accept-Encoding: gzip,deflate\r\n"
request = request + "Content-Type: application/soap+xml\r\n" if soapMsg.downcase.include?("soap-envelope")
request = request + "Content-Length: #{soapMsg.length}\r\n"
request = request + "Authorization: Basic #{password}\r\n"
request = request + "\r\n"
request = request + soapMsg
request = request + "\r\n"
log.debug("Trying to connect to SOAP service on #{host}:#{port}")
# Sending request
socket = TCPSocket.open(host,port)
log.debug("Sending request to SOAP service")
log.debug("#{request}")
socket.send(request, 0)
# Reading response
log.debug("Reading SOAP service response")
response = socket.read
headers,body = response.split("\r\n\r\n", 2)
log.debug("Received SOAP service response headers #{headers}")
log.debug("Received SOAP service response body #{body}")
log.debug("Closing SOAP service connection")
socket.close
log.debug("Connection with SOAP service closed")
return headers,body
end
When I make HTTP call, I get correct response back : Received SOAP service response headers HTTP/1.1 200 OK
but when I make HTTPS call, I get "Received SOAP service response headers §♥♥ ☻☻P".
Am I missing something ?

Related

OkHttp - keep getting the error: "content-type of request should be application/json"

Why do I get this error "content-type of request should be application/json", because I encoded it application/json?
How to correct it?
In Postman the request is working fine.
int id = 208;
MediaType JsonType = MediaType.parse("application/json");
OkHttpClient client = new OkHttpClient();
String jsonBody = "{\"params\":[\"wandelnet\"," + id + "]}";
RequestBody body = RequestBody.create(jsonBody, JsonType);
Request request = new Request.Builder()
.url( "https://wandelnet.api.routemaker.nl/routemaker/getPublishedRoute")
.post(body)
.addHeader("Content-Type", "application/json; charset=utf-8")
.addHeader( "Accept", "application/json")
.build();
Response response = client.newCall(request).execute();
The result is:
{"result":null,"error":{"code":"sherpaBadRequest","message":"content-type
of request should be application/json"}}
Try creating the request body from bytes, not from a string:
RequestBody body = RequestBody.create(jsonBody.getBytes(“UTF-8”), JsonType);
OkHttp automatically adds a charset when it does string encoding, and we need to prevent this here.
You’ll also want to omit the Content-Type header in your request builder.

OkHttp response 204 with content-type json

When i use okhttp3 ver 4.7.2 to create a json request with third api, I will receive response 204. But when I use asyncHttpClient to create the same json request, I will receive correct data.
This is my code using okhttp3:
MediaType JSON = MediaType.get("application/json");
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(interceptor).build();
RequestBody body = RequestBody.create("{}", JSON);
Request request = new Request.Builder()
.url(URL).post(body).build();
System.out.println("Start");
okhttp3.Response response = client.newCall(request).execute();
System.out.println(response.body().string()); // null
System.out.println(response.code()); // 204
This is my code using asyncHttpClient:
ListenableFuture<org.asynchttpclient.Response> whenResponse = client
.preparePost(URL)
.addHeader("Content-Type", "application/json").setBody("{}").execute();
org.asynchttpclient.Response response = whenResponse.get();
System.out.println(response.getResponseBody()); // Correct response body
System.out.println(response.getStatusCode()); // 200
Request json send by okhttp have conten-type is application/json; charset=utf-8, third api allow only application/json. So their response 204.

400 The plain HTTP request was sent to HTTPS port only from ruy

I Want To send a request to my website URL and get a response but only in ruby I'm getting this output
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>
I'm using also other languages like python, javascript, PHP but only in ruby I get this issue
require 'net/http'
require 'uri'
# insert your api key here
api = "my_api_key"
# replace my_email#my_email.com to email you want to validate
email = 'my_email#my_email.com'
# api url
uri = URI.parse( "https://emailvalidator.pawnhost.com/dashboard/api/"+ api +"/" ); params = {'email'=> email}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
request.set_form_data( params )
# instantiate a new Request object
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
response = http.request(request)
puts response.body
I want my API response but i get that

REST Assured returns response of "No HTTP resource was found that matches the request URI '****/api/Document/getBrowserData'"

I have implemented my REST Assured code like below
String tempUrl = "***/api/Document/getBrowserData";
Response rjson = given()
.param("dsId",1108)
.param("isNew", false)
.param("oToken","eed0361d314888a3f153534844869d9f")
.param("projId",837)
.param("tId",-1)
.param("type",2195)
.param("userId",104)
.post(tempUrl)
.then()
.extract()
.response();
System.out.println(rjson.asString());
and when I perform some action on the response:
System.out.println(rjson.asString()); // "Message":"No HTTP resource was found that matches
the request URI '
System.out.println(rjson.getHeaders()); // header value
System.out.println(rjson.getStatusCode()); // 404
But when I do the same thing with PostMan I am able to get a valid response.

View request text before sending

I'm using Net::HTTP to make a POST request:
uri = 'service.example.com'
https = Net::HTTP.new(uri)
https.use_ssl = true
path = "/service_action"
data = request_body_obj.to_json
response = https.post(path, data, {'Content-Type' => 'application/json'})
My request is timing out for some reason. To debug why, I'd like to see the text of the request body and headers of my request. Is there a way to do that?

Resources