api (write plugin) upload - A valid login session was not found

General Discussion
  • how upload via api?
    here the c# code:

    var restClient = new RestSharp.RestClient(baseUrlApiV2); 
    
    var request = new RestSharp.RestRequest("/util/upload", RestSharp.Method.POST);
    request.AlwaysMultipartFormData = true;
    request.AddHeader("Content-Type", "multipart/form-data");
    request.AddHeader("Authentication", token);
    request.AddHeader("Authorization", token);
    
    request.AddFile("file[]", @"D:\phpbbAtach\54_0df4f16285a8bce30059e548f506fdf1", "image/png");
    IRestResponse response = restClient.Execute(request);
    var content = response.Content.Dump();
    

    the raw request:

    POST http://mysite.com/api/v2/util/upload HTTP/1.1
    Content-Type: multipart/form-data
    Authentication: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
    Authorization: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
    Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
    User-Agent: RestSharp/106.2.1.0
    Host: tchumim.com
    Content-Length: 24977
    Accept-Encoding: gzip, deflate
    Connection: Keep-Alive
    
    -------------------------------28947758029299
    Content-Disposition: form-data; name="files[]"; filename="54_0df4f16285a8bce30059e548f506fdf1"
    Content-Type: image/png
    
    ......
    
    -------------------------------28947758029299--
    

    the response (401):

    {"code":"not-authorised","message":"A valid login session was not found. Please log in and try again.","params":{}}
    
  • Are you using Bearer authentication with a user or master token? If master token, a uid must be provided in the payload under the key _uid.

  • thank! right, its a master token, but even after I added the parameter, yet the same response!

    -------------------------------28947758029299
    Content-Disposition: form-data; name="_uid"
    
    1
    -------------------------------28947758029299
    Content-Disposition: form-data; name="file[]"; filename="54_0df4f16285a8bce30059e548f506fdf1"
    Content-Type: image/png
    
    ....
    -------------------------------28947758029299--
    
  • now i have tried with a user token, and same response. other call api works.

    nodebb version 1.7.3

  • I have no choice but to do so with forms-authentication 😞

  • @david-levtov version 1.8.2 same issue

  • This post is deleted!
  • @foyangyu curl -H "Authorization: Bearer fba206bd-8f0d-491e-83a9-b08383a4c575" --data "title='NEW topic TITLE'&content='CONTENT MORE THAN 8'&cid=4" http://localhost:4567/api/v1/topics

    how fully i am

  • Hi all,

    Confirmed working, here's the curl call I used:

    $ cd /directory/with/file/to/upload
    $ curl -L https://localhost/forum/api/v2/util/upload?_uid=1 -H "Authorization: Bearer e3704a27-9368-4a3c-b743-0dbc5da08e39" -F "files[][email protected]"
    

    Keep in mind my local dev uses a subfolder, that's why I am calling localhost/forum. You might call your own hostname, url, or localhost:4567/api...

    Here is the return payload:

    [
        {
            "url": "/forum/assets/uploads/files/1524840237214-filename.jpg",
            "path": "/home/julian/Projects/nodebb/forum/public/uploads/files/1524840237214-filename.jpg",
            "name": "filename.jpg"
        }
    ]
    
  • @julian said in api (write plugin) upload - A valid login session was not found:

    -F "files[]=@

    hi, I don't understand, when I want to update words and pics, how to write the curl ?
    thanks .

  • What did you try so far?

  • thanks, I try to use the
    curl -H "Authorization: Bearer {YOUR_TOKEN}" --data "title={TITLE}&content={CONTENT}&cid={CID}" http://localhost:4567/api/v1/topics
    to send posts, It's all right.
    Is there any samply about the "JSON Web Token " to send posts ?
    very thanks .

  • For anyone using NodeBB to upload a screenshot from node.js

    		const buffer = await createImage({
    			path: `screenshot/annotation:${ annotationID }`
    			, encoding: 'binary'
    		})
    
    		const result = await rp({
    			url: `${ endpoint }/util/upload?_uid=${ _uid }`,
    			method: 'POST',
    			json: true,
    			headers: {
    				Authorization
    				, 'Content-Type': 'multipart/form-data'
    			},
    			formData: {
    				'files[]': {
    					value: buffer,
    					options: {
    						filename: `annotaion-${ annotationID }.jpg`,
    						contentType: 'image/jpg'
    					}
    				}
    			},
    		})
    
    

Suggested Topics