Unable to upload attatchments over the Jira API - perl

I've seen the question asked a few times but no real answer. The Jira::Rest module appears to be out of date since username and password is not an accepted authentication method.
I am unable to add attachments via perl. My first attempt was using REST::Client with LWP::useragent. Example below
my $client = REST::Client->new();
$client->addHeader('Authorization', 'Basic ' . $config_yaml->{auth}
$client->addHeader('Content-Type', 'application/json');
$client->addHeader('X_Atlassian_Token', 'no-check');
$client->addHeader('Content' => [ file => [ $filename ], ]);
my $file_sent = $client->POST("issue/$ticket/attachments");
my $response = $file_sent->responseContent();
For some reason the response was an empty string.
I thought maybe I was missing something so I tried with a curl request
curl --request POST --url 'https://cloudurl.atlassian.net/rest/api/3/issue/[ticket_id]/attachments' \
--header 'Accept: application/json' --header "X-Atlassian-Token: no-check" \
--header 'Authorization: Basic [api auth string]' \
-F "file=#test_file_upload.txt"
The curl request returned the expected message and the ticket had the attachment. So I tried again using just LWP::UserAgent.
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->default_header('Accept' => 'application/json');
$ua->default_header('X-Atlassian-Token' => 'no-check');
$ua->default_header('Authorization' => 'Basic ' . $config_yaml->{auth});
my $response = $ua->post($url, Content_Type => 'multipart/form-data',
Content => [
field => [$filename],
],
);
warn $response->is_success;
warn $response->decoded_content;
For some reason this returned 200 and an empty just like when I tried with Rest::Client. I believe the URL and parts of it are correct.
If X-Atlassian-Token is ommitted I get the error specified in the documentation and if I send no content I get an error complaining about that missing. Is there something missing from my request when I make it via LWP::UserAgent or REST::Client that causes me to get no response?
For reference this is the part of the api documentation I have been trying to work from.
https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post
The parts discussed on the atlassian support area do not seem to have a solution for this either.

If you have root access on the box, I'd use tcpdump -A -s2048 host cloudurl.atlassian.net and look for any differences between the successful curl and the other messages.
https://www.perlmonks.org/bare/?node_id=464442 suggests using
print $response->headers_as_string;
or
print join "\n", $file_sent->->responseHeader($_) for $file_sent->->responseHeaders();
to see what you're getting back.

Related

Jenkins http plugin to upload file using rest

I am trying to upload a file to a rest server from jenkins using http plugin. I have a jenkins pipelie where a step involves loading a file(type formData)
to a server using rest.
the server side method uses two parameters:
(#FormDataParam("file") InputStream file, #FormDataParam("fileName") String fileName)
I am using the below method
def filename = "${WORKSPACE}/Test.txt"
data="""{ \"fileName\" : \"Test.txt\" }"""
resp3 = httpRequest consoleLogResponseBody: true,url: "http://<url>",contentType:'APPLICATION_OCTETSTREAM',customHeaders:[[name:'Authorization', value:"Basic ${auth}"]],httpMode: 'POST',multipartName: 'Test.txt',uploadFile: "${filename}",requestBody:data,validResponseCodes: '200'
but when I run the status code is 400 and in the server logs the message is that no filestream and no filename is received i.e not able to get both the arguments.
Please let me know where it is getting wrong
Regards
You can try using curl instead of built-in Jenkins methods:
curl -XPOST http://<url> -H 'Content-Type: application/octet-stream' -H 'Authorization: Basic ${auth}' --data-binary '{\"fileName\" : \"Test.txt\" }'
You can debug it first from within shell. Once it's working, wrap it in sh directive:
sh "curl ..."
Since I was running on windows so bat + curl worked for me .With this workaround I was able to transfer files using jenkins and rest
However using httpRequest from jenkins inbuild library is still not working.

How to send http post request with an Avro file?

I have a flask api that is expecting a post request in Avro. The problem is I'm not sure how to send Avro requests to test it. The api reads the data using the fastavro.reader(io.BytesIO(request.data))
I have tried using postman:
In the header defining Content-Type as "avro/binary"
However looks like its not possible
https://github.com/postmanlabs/postman-app-support/issues/4435
I also tried curl:
curl -X POST -H "Content-Type: avro/binary" --data "{"city": "ALA",
"number_of_points": 42, "transport": "CAR", "subtype": "PURCHASE"}"
"http://localhost:8080/invocations"
However fastavro returns the following error:
File "fastavro/_read.pyx", line 725, in fastavro._read.file_reader.init
ValueError: cannot read header - is it an avro file?
Resources:
https://github.com/fastavro/fastavro
https://avro.apache.org/
Okay, so I am assuming you have a valid .avro file, as per the example on the fastavro docs.
This then becomes a simple case of handling this as a standard file upload to Flask. So rather than taking the data from request.data you could so something like:
from fastavro import reader
from flask import Flask, request
app = Flask(__name__)
# This is really basic and could use some validation
#app.route('/invocations', methods=['POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
for record in reader(file):
print (record)
return 'uploaded'
You could then submit your file to the endpoint with curl:
curl -i -X POST -F 'file=#out.avro' "http://localhost:5000/invocations" -H 'ContentType: multipart/form-data'
This should result in something on the server console like:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
{'station': '011990-99999', 'time': 1433269388, 'temp': 0}
{'station': '011990-99999', 'time': 1433270389, 'temp': 22}
If you wish to submit using the requests library, you can do something like this:
import requests
def upload(filename):
headers={'ContentType': 'multipart/form-data'}
with open(filename,'rb') as f:
files = {'file': f}
url='http://localhost:5000/invocations'
r = requests.post(url, files=files)
print (r.content, r.status_code)
upload('out.avro')

How to get form-data from PUT request using Flask-restplus

One of the endpoints of my API handles PUT request with multipart/form-data. While an audio file is uploaded, some other data is sent from client in form-data. I am able to receive the file using the following code, but am having trouble getting the form-data.
#api.route('/upload')
class AudioUpload(Resource):
def put(self):
now = datetime.now()
filename = now.strftime("%Y%m%d_%H%M%S") + ".mp3"
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
name = request.form['name']
print('name: ', name, file=sys.stdout)
with open(filepath, 'wb') as f:
f.write(request.stream.read())
return filepath
The curl command I tested with is:
curl -X PUT \
http://localhost:5000/api/upload \
-H 'content-type: multipart/form-data \
-F file=#Audio-3791_244-Feb_04_2018-13_30_04.wav \
-F name=xyz
I got 400 with error The browser (or proxy) sent a request that this server could not understand.
What is the correct way getting form-data in PUT request?
EDIT
Just tried the same code with post. It does not work for getting the form-data either with same error.

how to do a put for camel-example-swagger-cdi?

I can do get on camel-example-swagger-cdi. But how do I do a put ?
I tried the following on browser, it didn't work at all.
http://localhost:8080/user/{'id':'222','name':'Richard'}.
And I tried the following curl command, it didn't work either
curl -X PUT -d id=222 -d name="Richard" localhost:8080/user
Please advise how I can issue a post
public class UserRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
// configure we want to use servlet as the component for the rest DSL
// and we enable json binding mode
restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json)
// and output using pretty print
.dataFormatProperty("prettyPrint", "true")
// setup context path and port number that netty will use
.contextPath("/").port(8080)
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
// and enable CORS
.apiProperty("cors", "true");
// this user REST service is json only
rest("/user").description("User rest service")
.consumes("application/json").produces("application/json")
.get("/{id}").description("Find user by id").outType(User.class)
.param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
.to("bean:userService?method=getUser(${header.id})")
.put().description("Updates or create a user").type(User.class)
.param().name("body").type(body).description("The user to update or create").endParam()
.to("bean:userService?method=updateUser")
.get("/findAll").description("Find all users").outTypeList(User.class)
.to("bean:userService?method=listUsers");
}
}
I used Postman to do a put:
localhost:8080/user?body={'id':'222' , 'name':'Richard' }
Got
Used Postman to do a put. Got
java.net.URISyntaxException: Illegal character in query at index 11: /user?body=
{%27id%27:%272222%27%20,%20%27name%27:%27Richard%27%20}
I got the following exception when I run curl to put.
$ curl -X PUT -d "body={'id':'222','name':'Richard'}" http://localhost:8080/user
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3142 100 3108 100 34 33419 365 --:--:-- --:--:-- --:--:-- 50129com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'body': was expecting ('true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream#17b2613c; line: 1, column: 6]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3451)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2610)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:841)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:737)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3776)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3721)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2796)
at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:173)
at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69)
at org.apache.camel.processor.binding.RestBindingProcessor.process(RestBindingProcessor.java:168)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.component.netty4.handlers.ServerChannelHandler.processAsynchronously(ServerChannelHandler.java:134)
at org.apache.camel.component.netty4.handlers.ServerChannelHandler.channelRead0(ServerChannelHandler.java:105)
at org.apache.camel.component.netty4.http.handlers.HttpServerChannelHandler.channelRead0(HttpServerChannelHandler.java:211)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler.channelRead0(HttpServerMultiplexChannelHandler.java:113)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:42)
at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:309)
at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at java.lang.Thread.run(Thread.java:724)
If you can use GUI tools you should take a look at Postman, otherwise you can run:
curl -X PUT -d "{'id':'222','name':'Richard'}" http://localhost:8080/user
I would also recommend you to take a look at PUT vs POST in REST, and Swagger's Parameter Object docs.
I figured it out. And it is working now. curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d "{ \"id\": 222,\"name\": \"Richard\"}" http://localhost:8080/user

PHP Curl CURLOPT_IPRESOLVE

I have been working on a facebook application which uses facebook graph API for authentication, recently facebook upgraded to IPv6 and my network does'nt support IPv6 so all of my calls started returning Host is unreachable error, I searched on facebook bugs and found that we can still use force the requests to facebook to use IPv4 using CURL CURLOPT_IPRESOLVE.
Now when I try to send request to Facebook Graph API using curl I get
Notice: Use of undefined constant CURLOPT_IPRESOLVE - assumed 'CURLOPT_IPRESOLVE'
I want to know how can I enable support for this constant or how can I disable IPv6 support in php so that I can send request to Facebook Graph API using IPv4.
My code is
$url = ("https://graph.facebook.com/me/access_token?tokrn");
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
Thanks
Check you curl version
CURLOPT_IPRESOLVE is available since curl 7.10.8
Try this sample code to test
<?php
$version = curl_version();
// These are the bitfields that can be used
// to check for features in the curl build
$bitfields = Array(
'CURL_VERSION_IPV6',
'CURLOPT_IPRESOLVE'
);
foreach($bitfields as $feature)
{
echo $feature . ($version['features'] & constant($feature) ? ' matches' : ' does not match');
echo PHP_EOL;
}
FYI: http://gcov.php.net/PHP_5_3/lcov_html/curl/interface.c.gcov.php