Upload file using curl command line in dokuwiki - file-upload

I am trying to find a way to do this.
So far I have been able to access pages thanks to the cookie parameter:
curl -v --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "u=login&p=passwd" http://wiki/doku.php?id=start&do=login
and then
curl --cookie cookies.txt http://wiki/doku.php?id=info
To upload a file I am supposed to get the form parameter on the php page. I don't exactly know what I am looking for :
curl --form "file=#z.xml" --cookie cookies.txt "http://wiki/doku.php?id=start&tab_files=upload&do=media"

I made it work differently.
I have created a upload.php file (source)
<?php
$uploaddir = '/var/www/html/dokuwiki/upload/';
$uploadfile = $uploaddir . basename($_FILES['upload']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['upload']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
And the curl command (source)
curl --form upload=#file --form press=OK http://wiki/upload/upload.php
To finish I have allowed a few hosts in httpd.conf
<Directory "/var/www/html/dokuwiki/upload">
Order allow,deny
Allow from 192.168.1

Related

Is it possible to use curl/json with tcl and retrieve spotify API data?

I'm trying to make a simple proc in TCL that gets data from SPOTIFY API about what track I'm currently playing.
Any ideas are welcome. :D
Format:
GET https://api.spotify.com/v1/me/player/currently-playing
I need to send this:
curl -X "GET" "https://api.spotify.com/v1/me/player/currently-playing?market=ES&additional_types=episode" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer BQAai*****8n-5zXrLypj********hsgafd"
(id code for auth masked)
Reference: https://developer.spotify.com/documentation/web-api/reference/
This is a variant of Donald's solution using Tcl's built-in http support:
package require http;
package require json;
package require tls
::http::register https 443 ::tls::socket
set spotifyToken "BQAai*****8n-5zXrLypj********hsgafd"
proc spotifyGet {api args} {
global spotifyToken;
set url https://api.spotify.com/v1/$api?[http::formatQuery {*}$args]
dict set hdrs Authorization [list Bearer $spotifyToken]
dict set hdrs Accept "application/json"
set token [http::geturl $url \
-type "application/json" \
-headers $hdrs]
if {[http::status $token] eq "ok"} {
set responseBody [http::data $token]
} else {
error "Spotify call failed: [http::error $token]"
}
http::cleanup $token
return [json::json2dict $responseBody]
}
set data [spotifyGet me/player/currently-playing market ES additional_types episode]
Not so complicated, after all, but requires an additional dependency: TclTLS.
As a first cut, here's a very simple wrapper:
proc curl args {
# The options skip showing stuff you don't want in scripted form
# They also enable following redirects; you probably want to do that!
exec curl -s -S -L {*}$args
}
# Usually easiest to keep this sort of thing separate
set spotifyToken "BQAai*****8n-5zXrLypj********hsgafd"
# -G to force the use of the GET verb
set json [curl -G "https://api.spotify.com/v1/me/player/currently-playing?market=ES&additional_types=episode" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $spotifyToken"]
We can wrap that further:
package require http; # Standard part of Tcl
package require json; # Part of tcllib
proc spotifyGet {api args} {
global spotifyToken; # Assume variable set as earlier
set url https://api.spotify.com/v1/$api?[http::formatQuery {*}$args]
set options {-H "Accept: application/json" -H "Content-Type: application/json"}
lappend options -H "Authorization: Bearer $spotifyToken"
return [json::json2dict [curl -G $url {*}$options]]
}
set data [spotifyGet me/player/currently-playing market ES additional_types episode]
puts [dict get $data item name]
If you're doing more serious JSON work, consider using the rl_json library. And you'll need to explore the data you get back to understand it. (I can't test; I don't use Spotify.)

How should a GraphQL request for file upload look like?

I use Graphene on the server side with similar code to the one from documentation:
class UploadFile(graphene.ClientIDMutation):
class Input:
pass
# nothing needed for uploading file
# your return fields
success = graphene.String()
#classmethod
def mutate_and_get_payload(cls, root, info, **input):
# When using it in Django, context will be the request
files = info.context.FILES
# Or, if used in Flask, context will be the flask global request
# files = context.files
# do something with files
return UploadFile(success=True)
It's all clear, but how should the request look like ?
I've seen people suggesting multipart/form-data, but AFAIK that requires additional layer to parse the multipart request, so that's probably not what I need.. or is it ? :
curl -X "POST" "http://127.0.0.1:5001/graphql" \
-H 'Content-Type: multipart/form-data; boundary=----GraphQLFileUpload' \
-F "operations={\"query\":\"mutation ($files: [Upload!]!) {uploadFile(selfie: $file) {status}}\",\"variables\":{}}" \
-F "map={\"x\":[\"variables.files.x\"]}" \
-F "x=#/tmp/dummy.jpg "
I'll reply myself. The curl code I had was based on an external library that confused the hell out of me.
Here's my solution that doesn't require any additional library:
Python server code (graphene):
class UploadImage(graphene.Mutation):
class Arguments(object):
file = graphene.String(required=True)
status = graphene.Boolean()
def mutate(self, info, file):
img = info.context.files[file].read()
# more stuff
return UploadImage(status=True)
Curl request (multipart form)
curl -X POST http://localhost:5001/graphql \
-H 'content-type: multipart/form-data; boundary=----GraphQlFileUpload' \
-F 'query=mutation {uploadImage(file: "photo") {status}}' \
-F 'photo=#selfie.jpg'

paypal authentication failure in sandbox

I encounter an issue when I try to use the Paypal sandbox API.
I've created my 2 sandbox accounts (the facilitator and the buyer), and I've created my app to get the credentials.
Then, I use the curl example provided by Paypal to get a token :
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "my-client-id:my-secret" \
-d "grant_type=client_credentials"
I get a 200 response, with an "access_token".
Then, I use this access token to get another resource, for example :
curl -v -X GET https://api.sandbox.paypal.com/v1/invoicing/invoices?page=3&page_size=4&total_count_required=true \
-H "Content-Type: application/json" \
-H "Authorization: Bearer the-token-received-above"
Then, I get a 401 error :
{
"name":"AUTHENTICATION_FAILURE",
"message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.",
"links":[{
"href":"https://developer.paypal.com/docs/api/overview/#error",
"rel":"information_link"
}]
}
I don't understand what I'm doing wrong, since I've followed every step decribed in the Paypal doc (at least, I think I have... probably not)
Thanks for your help
curl -v -X GET "https://api.sandbox.paypal.com/v1/invoicing/invoices?page=3&page_size=4&total_count_required=true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer the-token-received-above"
Actually had this exact same issue but didn't know what was wrong with my curl. For me, the issue was I forgot to put "Bearer" in the Authorization section.
For this, you are required to wrap the URL with quotation marks.
After get access_token. Please try this
try {
$params = array('access_token' => $jsonResponse->access_token);
$userInfo = OpenIdUserinfo::getUserinfo($params, $this->_api_context);
} catch (Exception $ex) {
ResultPrinter::printError("User Information", "User Info", null, $params, $ex);
exit(1);
}
ResultPrinter::printResult("User Information", "User Info", $userInfo->getUserId(), $params, $userInfo);
Don't forget to add
use PayPal\Api\OpenIdTokeninfo;
use PayPal\Api\OpenIdUserinfo;
That's worked for me.

write ansible-playbook for webservices

How do I use POST with Ansbile
curl -k -X POST -H "Accept:application/json" -H "Content-Type:application/json"-u user:A231212123432 "https://www.example.com"
Request Body
{
"a":"1"
"b":"2"
"c":"3"
}
According to the docs it would work like this:
- url: https://www.example.com
method: POST
user: user
password: A231212123432
HEADER_Content-Type: "application/json"
body: '{ "a":"1" "b":"2" "c":"3" }'

How to get variable in CGI made by /bin/sh + Apache

I'm coding following CGI script
echo "Content-type: text/html; charset=UTF-8\n\n"
echo "<HTML><HEAD><TITLE>title</TITLE></HEAD>"
echo "<BODY>"
echo "<FORM ACTION="http://exapmle.com/page2.cgi" NAME="PAGE1" METHOD="POST">"
echo "input:<INPUT TYPE=text NAME="data1" SIZE=10 MAXLENGTH=10>"
echo "<INPUT TYPE=submit NAME=nbtn VALUE='GO TO PAGE2'>"
echo "</FORM>"
echo "</BODY>"
echo "</HTML>"
How to get valiable in CGI made by /bin/sh + Apache
I am glad gime me sample cord
In the CGI script, you receive it as a parameter to main, so it should be in some place pointed by argv
Apache runs the cgi just like you:
script.cgi var1
And the parameters are in argv, so this:
printf("%s", argv[1]);
Will output
var1