Guzzle 6, put request and description json - guzzle

I use guzzle 6 with a json file to describe my methods to call.
Bellow, an example with a put request :
in the json descriptor file :
"putObjects" : {
"httpMethod": "PUT",
"uri": "objects",
"summary": "Send objects to the api",
"parameters": {
"objects" : {
"type" : "string",
"location" : "body"
}
}
}
in the symfony controller :
$clientResponse = $client->execute(
$client->getCommand("putObjects", array(
'objects' => $request->getContent()
))
);
Before, with guzzle 3 when the put request was sent, the data sent was formated like this (a valid json) :
{objects: [{....}]}
But now, with guzzle 6, the data is formated as :
objects = {objects: [{....}]}
And of course my api send me an error 'Invalid json message received'.
Someone has an idea about this issue ?

I have found the solution.
In symfony controller :
$content = json_decode($request->getContent(), true);
$clientResponse = $client->execute(
$client->getCommand("putObjects", array(
'objects' => $content['objects']
))
);
In the json descriptor file :
"putObjects" : {
"httpMethod": "PUT",
"uri": "objects",
"summary": "Send objects moderated",
"parameters": {
"objects" : {
"type" : "array",
"location" : "json"
},
}
}

Related

For OpenAPI Specification 3 how should I define the parameter of a request body

I want to define my API endpoints with swagger OAS 3.0.0. My API requires quite a few parameter in the request body and I would like to give a detailed explanation for each request body parameter. The older version of OAS allows for "path" : { "endpoint" : { "in" : "body"}}} which would be perfect because I can describe each parameter individually. However, for OAS 3.0.0 it is stated that parameter in request body should be defined using the requestBody field which does not support description(ie what is the parameter referring to) for each parameter. Is there anyway for me to describe each request body parameter in OAS 3.0.0.?
This way of defining parameters is perfect for me as my clients will be able to see the various parameters clearly.
"parameters" : [{
"name" : "phone_no",
"in" : "body",
"description" : "User mobile no. It should follow International format.",
"required" : true,
"explode" : true,
"schema" : {
"type" : "string",
"example": "+XXXXXX"
}
}, {
"name" : "signature",
"in" : "body",
"description" : "How to get signature ....",
"required" : true,
"explode" : true,
"schema" : {
"type" : "string",
"example" : "XXXXXXXXXXX"
}
} ]
Good readable request body parameters
According to OAS 3.0.0 I should define the parameters in this format which is not ideal as the rendered API documentation will clump the definition of the parameters together which would be less readable for the client.
"requestBody" : {
"description" : "HTTP Request Body",
"content" : {
"application/json" : {
"schema" : {
"properties" : {
"phone_no" : {
"type" : "string",
"required" : true,
"description" : "User mobile no. It should follow International format."
}, "signature" : {
"type" : "string",
"required" : true,
"description" : "How to get signature ...."
}
}
}
}
}
}
Less readable Request Body parameters

Swagger 2.0 uploading a file to SP Online that doesn't send the extra content through

I'm hoping someone can point out my mistake here.
I have the following swagger definition which I use on swaggerhub that will upload a file to Sharepoint document library via the rest api
{
"swagger" : "2.0",
"info" : {
"description" : "defaultDescription",
"version" : "2",
"title" : "defaultTitle"
},
"host" : "someSite.sharepoint.com",
"schemes" : [ "https" ],
"paths" : {
"/sites/ms/_api/Web/GetFolderByServerRelativeUrl('doc/test/tt')/Files/Add(url='{filename}',overwrite=true)" : {
"post" : {
"consumes" : [ "multipart/form-data" ],
"produces" : [ "application/json" ],
"parameters" : [ {
"in" : "formData",
"name" : "upfile",
"type" : "file",
"required" : true,
"description" : "The file to upload."
},
{
"in" : "path",
"name" : "filename",
"type" : "string",
"required" : true
} ],
"responses" : {
"200" : {
"schema" : {
"type" : "string"
},
"description" : "Definition generated from Swagger Inspector"
}
}
}
}
}
}
Problem is I can't open any files on SP because they're broken, and I believe I found the reason when I tested with a txt file.
I'll send a text file only containing Sample text bu when I open it on SP doc library it contains all the following as well
-------------------------------28947758029299
Content-Disposition: form-data; name="upfile"; filename="myt.txt"
Content-Type: text/plain
Sample text
-------------------------------28947758029299--
Is the issue with my content type or should I use the parameter differently, I tried researching this but what I found just matched the original guid I found
https://swagger.io/docs/specification/2-0/file-upload/
We came across a similar issue in our project.
The root cause is that Swagger 2.0 won't let you specify a different Content-Type for type file. You have to use multipart/form-data. See details here: https://swagger.io/docs/specification/2-0/file-upload/
To resolve this, you have to change the type from file to generic object. For example:
paths:
/sharepoint_upload/:
post:
description: "This will uploads a document to SharePoint."
operationId: "uploadDocuments"
consumes:
- "application/octet-stream"
produces:
- "application/json"
parameters:
- name: "documentBody"
in: "body"
description: "The actual document"
required: true
schema:
type: "object"
responses:
200:
description: "OK - Your request was successfully completed."
400:

Spring Cloud Contract provider return same as request

I'm working with two microservices using Spring Cloud Contract. One providing its contract, and the other one consuming it. In one scenario the provider response is the same that the request.
So the provider contract is like this:
Contract.make {
request {
method 'POST'
url '/provider/foo'
body(
"foo": $(regex("[a-zA-Z0-9]{20}"))
)
}
response {
status 200
body(
"fooResponse": fromRequest().body("\$.foo")
)
}
And the generated wiremock mapping:
{
"id" : "a80c0871-f4c0-49e3-8cc1-94de39899669",
"request" : {
"url" : "/provider/foo",
"method" : "POST",
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(#.['foo'] =~ /[a-zA-Z0-9]{20}/)]"
} ]
},
"response" : {
"status" : 200,
"body" : "{\"fooResponse\":\"{{{jsonpath this '$.foo'}}}\"}",
"transformers" : [ "response-template" ]
},
"uuid" : "a80c0871-f4c0-49e3-8cc1-94de39899669",
"scenarioName" : "scenarioReturnSameAsRequest",
"requiredScenarioState" : "Started"
}
But when my code calls to the provider, with foo as any text, the wiremock returns:
{
"fooResponse" : "{{{jsonpath this '$.foo'}}}"
}
How can I build a contract that responses the same parameters as the request body?
Edit
I tried with a fixed value on the response and works fine:
Contract.make {
request {
method 'POST'
url '/provider/foo'
body(
"foo": $(regex("[a-zA-Z0-9]{20}"))
)
}
response {
status 200
body(
"fooResponse": "fooValue"
)
}
Now wiremock return:
{
"fooResponse" : "fooValue"
}
Maybe is not supported getting from request a regex value?
I think the mapping should contain request.body instead of this. Also I wonder if you need to use 3 times a { or just 2 times. Or do you need to escape these?
Possible mapping:
"response" : {
"status" : 200,
"body" : "{\"fooResponse\":\"{{jsonpath request.body '$.foo'}}\"}",
"transformers" : [ "response-template" ]
},
See also the chapter JSONPath helper on http://wiremock.org/docs/response-templating
I had the same problem once. You can try to use value() like this:
"fooResponse": value(fromRequest().body('$.foo'))

JFrog Artifactory API query for object properties does not return requested detail

I am requesting label properties for docker artifact, perhaps the url is not correct? I get response object (json) but label properties are not included. Code example:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json;docker.label.com.company.info.build='*'",
{'Authorization' => 'Bearer <REDACTED>'})
if response.code.to_s == "200"
puts ("Artifactory response "+ response.body)
puts ("response object: "+response.inspect())
else
puts ("Artifactory request returned "+response.code.to_s)
end
Connecting to artifactory
Artifactory response {
"repo" : "dockerv2-local",
"path" : "/anonymizer/functional/manifest.json",
"created" : "2018-03-14T14:52:22.681-07:00",
"createdBy" : "build",
"lastModified" : "2018-03-15T15:52:34.225-07:00",
"modifiedBy" : "build",
"lastUpdated" : "2018-03-15T15:52:34.225-07:00",
"downloadUri" : "http://myrepo:8081/artifactory/dockerv2-local/anonymizer/functional/manifest.json",
"mimeType" : "application/json",
"size" : "1580",
"checksums" : {
"sha1" : "bf2a1f85c7ab8cec14b64d172b7fdaf420804fcb",
"md5" : "9c1bbfc77e2f44d96255f7c1f99d2e8d",
"sha256" : "53e56b21197c57d8ea9838df7cffb3d8f33cd714998d620efd8a34ba5a7e33c0"
},
"originalChecksums" : {
"sha256" : "53e56b21197c57d8ea9838df7cffb3d8f33cd714998d620efd8a34ba5a7e33c0"
},
"uri" : "http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json"
}
response object: #<Net::HTTPOK 200 OK readbody=true>
If I understand you correctly, you want to get the properties of the manifest.json file, "docker.label.com.company.info.build" in particular.
From looking at your command:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json;docker.label.com.company.info.build='*'",
It seems that you are using a semicolon to get the properties, which is not the right way. As you can see in this REST API, in order to use the get properties you should use the ampersand sign, so your command should look like:
response = Net::HTTP.get_with_headers("http://myrepo:8081/artifactory/api/storage/dockerv2-local/anonymizer/functional/manifest.json&docker.label.com.company.info.build='*'",

How to specify supported http operation for a resource in json-ld?

I'm new to JSON-LD and I was wondering if there is any way of specifying supported operation of a resource in JSON-LD without using Hydra's supportedOperation or supportedProperty.
Is there any way to specify the context something like :
{
"#context" : {
"#vocab" : "http://www.schema.org/",
"data" : "object",
"id" :"Number",
"name" : "alternateName",
"full_name" : "name",
"links" : {
"#id" : "URL",
"#type" : "collection"
},
"href" : "URL",
"rel" : "relatedTo",
"operation" : [
{
"href" : "http://example.com/resources/1/anotherresources/2",
"method" : "POST",
"expects" :[parameter list],
"required" : [list of mandatory arguments],
"fixed value" : [list of argument with fixed value for a resource]
}
]
}
Any guidance would be of great help..
No, you can't specify it in the context. What you can do, however, is to bind an operation to a property in a Hydra ApiDocumentation (example 10 in the spec) and reference it via an HTTP Link header.