Stripping quotes from response and transferring to request endpoint - groovy

I'm using SoapUI for testing some REST api. As a response I'm getting the URL that should be an endpoint of the next request.
I made the following Property Transfer step
source : myApiCall
property : response
target : myHttpCall
property : endpoint
Everything would be ok, but when transferred the endpoint looks like "www.myurl.com" (with quotes) and thus is invalid. How do I strip the quotes from there?
Raw response :
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 98
Content-Type: application/json; charset=utf-8
Expires: Tue, 25 Oct 2016 09:04:28 GMT
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Tue, 25 Oct 2016 09:04:28 GMT
"http://myurl.com/query?queryUid=90e97bdb-00a3-47c2-8809-c15ceec6ea1b"

The problem is that your Raw response include the " quotes in the String. Then you've two possible solutions, remove the " from the Raw response and keep using the same property transfer.
Or if you can not change the response then you can use a Groovy script testStep to get the Raw response and manipulated it to remove the additional " quotes before set the endpoint:
// get your api call
def myApiCall = context.testCase.getTestStepByName('myApiCall')
// get the raw response
def responseUrl = myApiCall.getPropertyValue('Response')
// since your response contains the `"` remove it
responseUrl = responseUrl.replace('"','')
// set the endpoint correctly
def httpCall = context.testCase.getTestStepByName('myHttpCall')
httpCall.setPropertyValue("endpoint",responseUrl)

Related

How to automate downloading attachment from Rest API GET response in SoapUI

I am using SoapUI 5.5.0 and I am trying to automate the download of an .xls attachment from a Rest API GET response.
It does not appear in the attachment tab of the response.
I tried adding "Enable MTOM | true" but the request stop working with
it.
I tried some groovy scripts but I didn't get anything out of what I tried.
**RAW RESPONSE**
HTTP/1.1 201
Set-Cookie: Design_Authorization=VeryLongToken; Max-Age=93600; Expires=Tue, 12-Jan-2021 22:33:22 GMT; Path=/Redacted; HttpOnly
Set-Cookie: JSESSIONID=bunchofnumbers; Path=/Redacted; HttpOnly
Content-Disposition: attachment; filename=SoapUI_Export_DD_20210111_153209.xls
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/vnd.ms-excel
Transfer-Encoding: chunked
After this, the response has a bunch of unreadable characters.
If I look at the XML tab I get this:
**XML RESPONSE**
<data contentType="application/vnd.ms-excel" contentLength="647680">0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7/CQAGAAAAAAAAAAAAAAAKAAAAAAAAAAAAAAAAEAAA/v///wAAAAD+////AAAAAAEAAACAAAAAAAEAAIABAAAAAgAAgAIAAAADAACAAwAAAAQAAIAEAAD///...it's very long
Adding this here since I could not get a readable format in my thank you comment below.
I had a null error on the response.getProperty('Content-Disposition').split('=')[1] line.
Since I generate and store the name of the export earlier in the testcase, I get the property and then use it.
This is what I ended with:
import org.apache.commons.io.FileUtils
def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()
// define filepath/name
exportname = testRunner.testCase.getPropertyValue("exportName")
reportfolder = (System.getProperty("user.home") + File.separatorChar + "Documents" + File.separatorChar);
def filename = reportfolder + exportname +'.xls'
def file = new File(filename)
FileUtils.writeByteArrayToFile(file, data) `
The body of the response is the file. You have to extract it, something like this:
import org.apache.commons.io.FileUtils
def testStep = testRunner.testCase.testSteps['test step name']
def response = testStep.testRequest.response
assert response.getContentType() == 'application/vnd.ms-excel'
def data = response.getRawResponseBody()
// define some filename
def filename = response.getProperty('Content-Disposition').split('=')[1]
def file = new File(filename)
FileUtils.writeByteArrayToFile(file, data)

HTTP 200 Response not as expected, Python

I'm writing a program that creates the HTTP Request and sends i securely using SSL. It uses the python library SOCKET to send/receive data. The issue I'm having is that I'm receiving a 200 status back, but the body is not as expected. I've ran the same URI through POSTMAN with no issues.
Here is the full response in bytes (Bytes on wire are smaller than buffer (4096):
b'HTTP/1.1 200 OK\r\nDate: Wed, 20 May 2020 23:42:55 GMT\r\nServer: Apache\r\nStrict-Transport-Security: max-age=315
36000; includeSubDomains\r\nCache-Control: no-cache="Set-Cookie, Set-Cookie2"\r\nAccept-Ranges: bytes\r\nVary: Accep
t-Charset,Accept-Encoding,Accept-Language,Accept\r\nX-Frame-Options: SAMEORIGIN\r\nX-UA-Compatible: IE=edge\r\nTrans
fer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n845\r\n'
I'm expecting JSON data following 845\r\n like i see in my other responses but nothing.
My code for sending the request is:
'''
for i in nic_list:
try:
create_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
bind_addr_socket = create_socket.bind((i, 0))
with socket.create_connection((net_loc, port)) as sock:
with context.wrap_socket(sock, server_hostname=net_loc) as ssock:
parse = urlparse(type)
http_type = 'GET {} HTTP/1.1\r\nHost:{}\r\nAuthorization: Basic {}\r\nX-auth-access-token: {}\r\nX-auth-refresh-token: {}\r\nContent-Type: text/html\r\n\r\n'.format(parse[2], net_loc, creds, access_token, refresh_token)
ssock.sendall(http_type.encode())
response = ssock.recv(4096)
except (socket.gaierror, ValueError) as error:
continue
'''
The http_type variable produces the follow HTML, I've modified the output below for confidentiality:
GET /api/fmc_config/v1/domain/ededededed-e0f2-11e3-8169-6d9ed49edef/policy/accesspolicies/000C29F5-FE9F-0ed3-0000-691
489779138/accessrules/0rffr29F5-FE9F-0ed3-0000-derfrfrfr524550 HTTP/1.1
Host:10.1.1.1
Authorization: Basic cfencklnelfkcnelkfnv=
X-auth-access-token: djeidjkfjrjf-frfjirfj
X-auth-refresh-token: frhfuhrufhrifh-fjrifjrifjirfj
Content-Type: text/html

nodejs http post request instagram follow/unfollow

I have a script that uses cookies to make instagram post requests.
Problem: My follow requests dont work but my unfollow does
Works (statuscode = 200)
var url = 'https://www.instagram.com/web/friendships/19237590/unfollow/';
Doesnt Work (statuscode = 302)
var url = 'https://www.instagram.com/web/friendships/19237590/follow/';
So a status code of 302 is a redirect, it should provide another URL in a location header. e.g.
HTTP/1.1 302 FOUND
Connection: keep-alive
Server: meinheld/0.6.1
Date: Sat, 27 Jan 2018 10:51:09 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Location: http://example.com
You can then follow this..
If you want to try this out on another URl try:
https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F

how to read response header values in feature file of karate framework?

example:
I am getting response as
HTTP/1.1 200 OK
X-Backside-Transport: OK OK,OK OK,OK OK
Server: Apache-Coyote/1.1
eventid: 24FCE4D8FA4E6E1212E71960612312321
uuid: ec00d8f0-b168-489e-996e-234234234wer
how to retreive the response header value of field eventid? it tried as " def event = response.eventid "in feature file but getting "pathnotfoundexception"
Read the documentation: responseHeaders.
* def eventId = responseHeaders['eventid'][0]

Spotify Auth + HttpPost + Gson + Client Credential Flow

I'm developing a small app to learn about request/response/gson/etc.
What i'm trying to do is: Get the token through Client Credential Flow, to get some audio features.
I followed the guide:
https://developer.spotify.com/web-api/authorization-guide/#client-credentials-flow
And worked on the same idea as the spotify api:
https://github.com/thelinmichael/spotify-web-api-java/blob/master/src/main/java/com/wrapper/spotify/SpotifyHttpManager.java
Tried diff's stuffs, but i'm stucked now.
Currently, the error is:
HttpResponseProxy{HTTP/1.1 415 Unsupported Media Type [Server: nginx, Date: Thu, 02 Mar 2017 19:59:45 GMT, Content-Length: 888, Connection: keep-alive, Keep-Alive: timeout=600] ResponseEntityProxy{[Content-Length: 888,Chunked: false]}}
Here's the actual code:
BASE64Encoder base64Encoder = new BASE64Encoder();
String encodedClientIdKey = base64Encoder.encode((clientId + ":" + clientSecretKey).getBytes());
HttpPost httpPostRequest = new HttpPost("https://accounts.spotify.com/api/token");
httpPostRequest.setHeader("Content-Type", "application/json");
httpPostRequest.addHeader("Authorization", "Basic " + encodedClientIdKey);
JsonObject json = new JsonObject();
json.addProperty("grant_type", "client_credentials");
StringEntity stringEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
httpPostRequest.setEntity(stringEntity);
HttpResponse post = httpClient.execute(httpPostRequest);
I thought it was a content type problem, but couldn't solve by setting it on header or entity.
Any ideas?
PS:
Tried using form urlencoded instead of json as content type, here's the code (tried add content on header then as parameter as well):
BASE64Encoder base64Encoder = new BASE64Encoder();
String encodedClientIdKey = base64Encoder.encode((clientId + ":" + clientSecretKey).getBytes());
HttpPost httpPostRequest = new HttpPost("https://accounts.spotify.com/api/token");
httpPostRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPostRequest.addHeader("Authorization", "Basic " + encodedClientIdKey);
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
httpPostRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse post = httpClient.execute(httpPostRequest);
System.out.println(post);
And the error was Bad request this time:
HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: nginx, Date: Thu, 02 Mar 2017 20:14:50 GMT, Content-Type: application/json, Content-Length: 70, Connection: keep-alive, Keep-Alive: timeout=600] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 70,Chunked: false]}}

Resources