Possible to create a REST call with dynamic headers?

0
Hi, For a project, I’m currently evaluating a way to process REST calls to multiple providers. Say provider A requires custom header ‘X-CUSTOM-HEADER-1: 12345’ and provider B requires custom header ‘X-CUSTOM-HEADER-2: 67890’. I want to store the required headers in the database, so when I issue a request, I fetch the required headers and send them along with the REST call. As far as I can see, I can only add predefined headers to the REST call (only the values can be changed). Can I construct a custom request including the headers I want or do I need to create a custom java block to be able to get this working?
asked
2 answers
0

Mendix 8.9 did add registerHttpRequestInterceptor so you can intercept REST calls and manipulate the request in Java. You could use this to add your add your custom headers. 

https://apidocs.rnd.mendix.com/8/runtime/com/mendix/http/Http.html#registerHttpRequestInterceptor(java.lang.String,com.mendix.http.IHttpRequestInterceptor)

answered
1

In case anyone is interested, the java code below accepts input:
Goal was to be able to give ‘dynamic’ headers to the http request. Like in some cases the requests should give header ‘X-CUSTOMHEADER-SECRET: 123456’ and in other requests (to another host), it should give header ‘X-ANOTHERCUSTOMHEADER-SECRET: 67890’ in the request. In this way I can create the headers dynamicly and am not forced to give each variation its own REST call mendix block.

Uses com.mendix.http ( executeHttpRequest )

The java action takes the following values as input:
- url (string)
- method (string) – should be any of the constants from com.mendix.http.HttpMethod
- list of header objects
- payload (string)

Output is mendix object System.HttpResponse

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.http.*;
import java.net.URI;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;

public Execute_HTTP_Request_Mendix(IContext context, java.lang.String Url, java.lang.String Method, java.util.List<IMendixObject> Headers, java.lang.String Content)

// BEGIN USER CODE
  int total_headers = Headers.size();
  HttpHeader[] __Headers_java = new HttpHeader[ total_headers ];
  if( total_headers > 0 ){
    int cntr = 0;
    for( system.proxies.HttpHeader __HeadersElement : this.Headers ){
      __Headers_java[ cntr ] = new HttpHeader​( __HeadersElement.getKey(), __HeadersElement.getValue() );
      cntr++;
    };
  }
  URI __Uri_java = new URI( this.Url );

  HttpMethod __Method_java = HttpMethod.valueOf( this.Method );
    
  HttpResponse __Response_java = null;
  if ( this.Method == "GET" ){
    __Response_java = Core.http().executeHttpRequest​( __Uri_java, __Method_java, __Headers_java, null );
  }
  else{    __Response_java = Core.http().executeHttpRequest​( __Uri_java, __Method_java, __Headers_java, IOUtils.toInputStream( this.Content ) );
  }
 
  system.proxies.HttpResponse __Response = system.proxies.HttpResponse.initialize( getContext(), Core.instantiate( getContext(), "System.HttpResponse" ) );

  InputStream is = __Response_java.getContent();

  __Response.setContent( IOUtils.toString( is, StandardCharsets.UTF_8 ) );

  __Response.setStatusCode( __Response_java.getStatusCode() );

  return __Response.getMendixObject();
// END USER CODE

answered