Upload file to WordPress Site using Processing

Hi all, hoping somebody might be able to point me in the direction as im struggling to find a way to upload a json file to a WordPress site. Im currently trying to post the file to the "/wp-json/wp/v2/media/" location using the HTTP-Requests-for-Processing library but im getting back some errors and to be honest im not really sure what im doing any more lol!

Was hoping someone might have some ideas as to the best method of uploading a json file to the wordpress uploads location so i can hopefully use the json with a sketch that will run on the site.

I will post an example of the code i have been trying :)

Answers

  • edited May 2018
    import http.requests.*;
    
    void setup()
    {
      size (100, 100);
    }
    
    void draw()
    {
      PostRequest post = new PostRequest("https://" + "websitename.com/wp-json/wp/v2/media/");                             
      post.addFile("test.json", "C:/JSON Data/test.json");
      post.addHeader("Content-Type", "application/json");
      post.addHeader("username", "username");                                                                                         
      post.addHeader("password", "password");
      post.send();
    
      System.out.println("Reponse Content: " + post.getContent());
      System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
    
      noLoop();
    }
    
  • While the code doesnt raise any errors in processing i get the following response:

    Reponse Content: {"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}

  • Any help with best options to upload files to wordpress using processing would be greatly appreciated.

  • Little experience but you could serialized ypur data and add it to a get request and the process it in WP side. This relies in two assumptions:

    • your file is not big. I think the limit is 2k or 3k chars.
    • you can do php in the WP side.

    This is an idea. There could be better ways to do this and hopefully you get some other suggestions here.

    Kf

  • Thanks Kf.

    Ideas are what im looking for at the moment as im worried i might be barking up the wrong tree by heading in the route i am.

    The JSON files i will be uploading are not massive but i believe some will be bigger than 2/3k. I will have a look into the idea though. It's been a while since ive done PHP but it shouldn't be an issue if i need to have some on the WP side.

    Thanks,

    Tom

  • Got it working in the end:

    import http.requests.*;
    import javax.xml.bind.DatatypeConverter;
    
    void setup()
    {
      size (100, 100);
      uploadFile();
      exit();
    }
    
    void uploadFile()  //post JSON file to Wordpress
    {
      String b64 = DatatypeConverter.printBase64Binary("USERNAME:PASSWORD".getBytes());
    
      PostRequest post = new PostRequest("UPLOAD_URL");                                      
      post.addHeader("Authorization", "Basic " + b64);
      post.addHeader("Cache-Control", "no-cache");
      post.addHeader("Content-Type", "multipart/form-data");                        
      post.addHeader("Content-Disposition", "attachment; filename=\"test.json\"");                          
      post.addFile("test.json", "LOCAL FILE LOCATION... /JSON Data/test.json");
      post.send();
    
      System.out.println("Reponse Content: " + post.getContent());
      System.out.println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
      noLoop();
    }
    

    Can maybe be refined but does the job. It uses basic auth for now but the next step is to secure it further using OAuth, something that doesnt seem to have been done from in Processing before.

  • Great, thxs for sharing.

    Kf

  • No problem. Am now trying to complete the same process with Oauth Authentication rather than Basic Authentication, it's like trying to turn a glider into the USS Enterprise lol!

Sign In or Register to comment.